M
marko.suonpera
How to create a buffer of memory in C++, whose size can dynamically
grow and shrink as needed? This is used for buffering input/output.
Several variable types, such as int and double are read and written in
this buffer in specific order.
Currently I use a fixed size array: char *data = new char[bufsize];
I thought about std::vector<char> but can I directly access the memory
in vector in this fashion:
*((int*)(data + writepos)) = intvalue;
writepos += sizeof(int);
*((double*)(data + writepos)) = floatvalue;
where writepos is the position, measured in bytes, inside the buffer?
If not, how can I write pos ints and doubles in vector<char>?
grow and shrink as needed? This is used for buffering input/output.
Several variable types, such as int and double are read and written in
this buffer in specific order.
Currently I use a fixed size array: char *data = new char[bufsize];
I thought about std::vector<char> but can I directly access the memory
in vector in this fashion:
*((int*)(data + writepos)) = intvalue;
writepos += sizeof(int);
*((double*)(data + writepos)) = floatvalue;
where writepos is the position, measured in bytes, inside the buffer?
If not, how can I write pos ints and doubles in vector<char>?