hey.
alright, i am building two different streambuf derived classes atm and have a few problems figuring out proper memory management. as a buffer, i use a char array. first question: at what point should i initialise it? or should i rather use a vector to do memory management for me? currently my constructor for buffer to produce random numbers looks like:
1 2 3 4 5 6 7 8 9
|
randbuffer::randbuffer(int min, int max, unsigned seed)
:
d_min(min),
d_max(max),
d_buffer(0)
{
srand(seed);
setg(0, 0, 0);
}
|
where d_buffer is a char*. as i ve understood the internet, i now have to implement the underflow member function, in which i feel i have done a bunch of bs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
int randbuffer::underflow()
{
if(gptr() < egptr())
return *gptr();
d_buffer = new char[30];
char *base = d_buffer;
char *start = base + 15;
ostringstream o_str;
o_str << rand() % (d_max - d_min) + d_min;
size_t n = o_str.str().copy(start, 0, o_str.str().length());
setg(base, start, start + n);
return *gptr();
}
|
i use a specific istream for rnd numbers. i simply tied a randbuffer to that stream via the rdbuf member:
1 2 3 4 5 6 7
|
inline irandstream::irandstream(int maxRnd)
:
rndbuffer(0, maxRnd, 1)
{
rdbuf(&rndbuffer);
fill(' ');
}
|
all code compiles fine.
BUT: i either get segmentation faults or nothing happens.
a better understanding of how i should handle the actual buffer in the streambuf would solve a couple of problems i feel, if anyone can help: thank you very much!
2nd: to produce rnd numbers using the mention irandstream class derived from istream, is it actually enough to just tie the randbuffer to the stream and extract? it should be a relatively short / easy implementation, as i ve been advised to implement everything inline. i cant quite figure out what i have to do extract the 'complete' rdn number, eg 128, in one step using the extraction operator or if i actually have to override it. the problem is that thanks to my first problem, testing and working on it isnt really an option right now.
Thanks to everyone who took the time to read!
Double thanks to anyone who can help °_*