|
Boost Users : |
Subject: Re: [Boost-users] [Interprocess] Problem with reserve for vector ofweak_ptr
From: Gaetan Gaumer (gaetan.gaumer_at_[hidden])
Date: 2010-05-31 03:46:10
2010/5/28 Cliff Green <cliffg_at_[hidden]>:
>> 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
Ok, so I't my fault ;)
I use mainly online reference (sgi.com and cplusplus.com ) and the
fact that reserve() does not
initialize memory is not so obvious in these docs.
So thank you for your help.
Gaetan.
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