dynamically sized block of memory

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>?
 
N

n2xssvv g02gfr12930

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>?

I suggest you take a look at the STL library which provides many well
tried and tested containers and algorithms which will mean less coding.
You might particularly want to look at using the I/O stream classes
which with overloading should enable the coding of safe reading and
writing of data to and from files.

JB
 
D

Daniel T.

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>?

vector< char > data( bufsize );

*((int*)(&data[0] + writepos)) = intvalue;
writepos += sizeof(int);
*((double*)(&data[0] + writepos)) = floatvalue;

However, there has got to be a better way using streams. Maybe a
stringstream opened in binary mode instead.
 
M

Michiel.Salters

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>?

You couldn't do this in C, and you can't do this in C++, because the
alignment
of double isn't guaranteed to be sizeof(int) - it might be
sizeof(double).

The solution is to use std::copy( data+writepos,
data+writepos+sizeof(int),
(char const*) (&intvalue));

It's not a vector<char> problem, though. &front() points to a normal
char[ ]
(which of course can move after a resize()). That's a perfect
replacement
for your char* data. Or use &Myvec[writepos] - that too is a valid
char*.

Note that you stll need to resize(bufsize) if you copy to begin() or
&front();
std::vector only grows automatically on insert()ions. std::copy can
insert()
if you use an insert_iterator - see std::back_inserter().

HTH,
Michiel Salters



HTH,
Michiel Salters
 
A

AnonMail2005

Check out the boost iostream library. It has a number of implemented
source/sinks for io. You can attach these various source/sinks to an
std::iostream and then input and output to them in a normal fashion
without knowing what the underlying source/sink is.

Two very useful pre-defined source/sinks are an in memory array,
and the null source/sink. From an in memory array, it is not too
hard to have a source/sink that grows/shrinks as needed.

As a first cut, try having a vector of pointers to fixed sized memory
chunks to implement a "buffer" that can grow/shrink. It is straight
forward to code this given the very simple interface provided by the
boost iostreams library. All of this would be, of course, hidden to
user of the iostream.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top