This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [Patch]New feature to all kind of stl container.vector::data_move() returns vector::data() that can be moved,just as unique_ptr::release() to unique_ptr::get()


On 8 January 2015 at 16:07, Federico Terraneo wrote:
> My (perhaps limited) understanding of how vector works is that it
> allocates objects with placement new on a potentially larger chunk of
> memory due to capacity() being possibly greater than size().
>
> So assuming a client would attempt to call data_move() on a vector of
> objects that need a destructor to be disposed of, he/she would have a
> hard time to safely deallocate that array of objects, as neither
> delete[] nor delete would be appropriate. The proper way to do it
> would be to call size() on the vector before data_move(), store the
> number of elements somewhere, and when it's time to dellocate the
> objects call placement delete on the first size() elements, afterwards
> deallocate the chunk of memory. Not impossible, but not intuitive.
>
> Moreover, vector allocates its memory using an allocator, not directly
> through new/delete so even in the case of a vector of int or other
> destructor-less data types, using delete[]/free() would be inappropriate.

Yes, what you need to do it properly is a type that stores the
allocator, stores the number of elements allocated (the capacity) and
stores the number of elements constructed (the size).

That type already exists and is called std::vector.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]