Boost logo

Boost Users :

Subject: Re: [Boost-users] [Interprocess] Problem with reserve for vector ofweak_ptr
From: Cliff Green (cliffg_at_[hidden])
Date: 2010-05-28 12:20:44


> Resize can trim the vector and I don't want it.
> I think "reserve" is the right function for my need.
>
> When I use push_back instead of operator[] there is no pb.

As Ion is probably just pointing out, there's a fundamental difference
between resize and reserve. Resize modifies the "logical size" of the
container, creating (by default construction) new entries, or removing (with
destruction) entries. Reserve does not change the "logical size", and it's
only use is as an implementation optimization (you can pre-assign internal
buffer space, so that when adding new entries later on a memory allocation
will already have occurred).

For a vector, you can create one and then reserve one all you want, but the
size is still 0 (until "push_back" or "insert", etc is performed), and
indexing into the vector will be out of bounds.

Read Josuttis C++ standard library book, or any good reference on the
difference.

Cliff
 


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net