Re: Preallocate ostringstream buffer

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"Jeff F" <not@xxxxxxxxxxxx> wrote in message
news:eR5h3RbWGHA.3492@xxxxxxxxxxxxxxxxxxxxxxx

Tom Widmer [VC++ MVP] wrote:
Andrew Jarvis wrote:
Dear All,

I want to write about 8Mb of data to a ostringstream and I do not
want the stream continually reallocating ever larger chunks of
memory as the data is written.

With ostrstream I can map the stream onto a preallocated buffer, eg.

char *buffer = new char[length];
ostrstream os(buffer, length);

Is a this possible with ostringstream?

No, sadly there is no ostringstream::reserve. Either use ostrstream
(assuming you don't need wide characters) or roll your own.

And the latter is particularly easy with the boost iostreams library, see:
http://www.boost.org/libs/iostreams/doc/index.html.

And it's generally unnecessary with the library shipped with VC++
these days, since the buffer grows exponentially. Execution time
is not a problem. If space limitations are a problem, it's still
particularly easy to use strstream with a preallocated buffer.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


.



Relevant Pages

  • Re: Preallocate ostringstream buffer
    ... I want to write about 8Mb of data to a ostringstream and I do not ... memory as the data is written. ... With ostrstream I can map the stream onto a preallocated buffer, ...
    (microsoft.public.vc.stl)
  • Re: Preallocate ostringstream buffer
    ... I want to write about 8Mb of data to a ostringstream and I do not want the stream continually reallocating ever larger chunks of memory as the data is written. ... With ostrstream I can map the stream onto a preallocated buffer, ...
    (microsoft.public.vc.stl)
  • Preallocate ostringstream buffer
    ... I want to write about 8Mb of data to a ostringstream and I do not want the ... stream continually reallocating ever larger chunks of memory as the data is ... With ostrstream I can map the stream onto a preallocated buffer, ...
    (microsoft.public.vc.stl)
  • Re: ostrstream initial size.
    ... >>ostrstream has been deprecated. ... > There is no way to reserve space in an ostringstream. ... > indeed often a manual buffer is maintained. ... allows one to easily create a stream and streambuffer that uses a ...
    (microsoft.public.vc.stl)
  • Re: Calling external program in C++
    ... hack at overhauling some code I wrote _years_ ago that used an ... ostrstream, so it might benefit from some cleanup): ... ostringstream &) ...
    (comp.lang.cpp)