This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
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()
- From: Federico Terraneo <fede dot tft at hotmail dot it>
- To: libstdc++ at gcc dot gnu dot org
- Date: Thu, 8 Jan 2015 17:07:59 +0100
- Subject: 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()
- Authentication-results: sourceware.org; auth=none
- References: <tencent_1737FA275BA843B20700F485 at qq dot com> <alpine dot DEB dot 2 dot 11 dot 1501080815100 dot 1564 at laptop-mg dot saclay dot inria dot fr> <CAH6eHdRJ7XGw1m6ntK+sBtyM70yvvqUmz-LxQdPrUH8bT4SY7g at mail dot gmail dot com> <alpine dot DEB dot 2 dot 11 dot 1501081246410 dot 31658 at stedding dot saclay dot inria dot fr> <tencent_215CF4C848ACCCEF53C7F318 at qq dot com> <CAH6eHdSY1_YDN1DMRCjnFRFLsn2Ju9ScJD7VoYJFpfEkwiurTw at mail dot gmail dot com>
On 08/01/2015 14:36, Jonathan Wakely wrote:
> On 8 January 2015 at 13:31, éååå <805600352@qq.com> wrote:
>> Same as my original idea. my vector<T>::data_move() can be
>> exactly in this function form T* data()&&;
>
> That is a poor interface, which can cause currently working code to
> leak memory.
>
> extern "C" void some_function(const int*, size_t); std::vector<int>
> get_values(size_t n); ... some_function(get_values(5).data(), 5);
>
> Today this works correctly. With your suggestion the call to
> data() would release ownership of the memory and it would never be
> freed.
>
>> And thanks to Marc Glisse for works on std::string. Since it's a
>> real nead,anyone consider my idea acceptable to libstdc++?
>
> Definitely not in your suggested form, no.
>
> The function must return some RAII type, which would probably make
> it incompatible with existing uses of data(), so it should have a
> different name.
>
>
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.
To me, it looks like a data_move() function would be hard to use
correctly.