Boost logo

Boost :

From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2006-08-11 12:09:25


Hi Dmitry,

Dmitry Smirnov wrote:
> Hi, Ion!
>
> It seems I found the difference between "resize" and "reserve"
>
> After changing resize to reserve in following code
> m_objSendVectorStream.tellp() will return -1 instead of size of data
> packet :-(
>
>
> m_objSendVectorStream.resize( QueueSettings::maxMessageSize() );
>
> m_objSendVectorStream.seekp( 0 );
> {
> boost::archive::defOArchive objOArchive( m_objSendVectorStream );
> objOArchive << objData;
> }
>

In theory, "reserve()" means "reserve memory to avoid reallocations" but
does _not_ insert characters in the stream, so the stream size will be
zero. I mean:

-> "resize(resize_size)" called "resize(resize_size)" in the underlying
vector, so vector was zero-filled (vector.size() == resize_size) and
thus, the result was that '0' characters were introduced in the stream
and the position was reset to 0.

->"reserve(reserve_size)" calls "reserve(reserve_size)" in the vector,
so memory is allocated but no characters are introduced in the vector
(vector.size() == 0), and the position is reset to zero, so you would be
in the end of the stream (position 0 == eof since there are no
characters). The formatted stream is unchanged but you guarantee no
allocations.

So:

basic_vectorstream<...> vstream;
vstream.resize(1000);

was equivalent to inserting 1000 '0' characters in the stream and to
reset the position. I wanted to avoid this, because the only way to
write to the stream should be though stream functions.

basic_vectorstream<...> vstream;
vstream.reserve(1000);

should be equivalent to just creating the vectorstream (so there are no
characters in the stream).

I need to investigate if tellp returning -1 in this case is correct
(should it return 0 or eof?)

My intention was to make this equivalent to creating a file.:

std::fstream file("file, std::ios_base::in | std::ios_base::out);
file.tellp() == ?

Hmmm,you were just telling me what the difference is or you consider
this a bug?

Regards,

Ion


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk