In-memory array output stream?

L

Laurens

Hi,


My application needs to create in-memory arrays, the size of which is not
known, or very hard to calculate, in advance. I would like to stream the
data using the standard I/O class framework. The class should take care of
the underlying memory management. Now which class is most suitable for this?
(Assuming there is one.) Will std::eek:strstream do the trick?

In Java I use java.io.ByteArrayOutputStream to create in-memory data.


Thanks
-Laurens
 
J

Jeff Schwab

Laurens said:
Hi,


My application needs to create in-memory arrays, the size of which is not
known, or very hard to calculate, in advance

On second reading, I assume you mean "in advance of the I/O I'm about to
tell you about."
I would like to stream the
data using the standard I/O class framework. The class should take care of
the underlying memory management. Now which class is most suitable for this?
(Assuming there is one.) Will std::eek:strstream do the trick?
Yes.

In Java I use java.io.ByteArrayOutputStream to create in-memory data.

When you spit the string from the ostrstream into the output stream,
make sure you flush the buffer. Most of the standard c++ streams (cout,
clog, fstream's, etc.) are buffered by default.
 
D

Dietmar Kuehl

Laurens said:
My application needs to create in-memory arrays, the size of which is not
known, or very hard to calculate, in advance. I would like to stream the
data using the standard I/O class framework. The class should take care of
the underlying memory management. Now which class is most suitable for this?
(Assuming there is one.) Will std::eek:strstream do the trick?

Yes, it would. However, you should probably rather use 'std::eek:stringstream'
because 'std::eek:strstream' is deprected: it has several serious problems
which 'std::eek:stringstream' has not. The only issue which 'std::eek:strstream'
solves which 'std::eek:stringstream' does not is writing to a buffer supplied
by the user. However, a corresponding stream buffer is easy to write and
not what you need if you can't predict the size of the generated output.
 

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
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top