This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC 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]

[Bug c++/60371] std::vector::emplace_back


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60371

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to ÐÐÐÑÐ ÐÐÐÐÑÐÐÐ from comment #3)
> Indeed, adding
> 
>   z (const z& x) { var = strdup (x.var); }
> 
> solves the problem.  However, I don't understand how that "y.clear();"
> between the y.emplace_back() in the original program avoids the double free.

In the original program the vector is resized on the second insertion, so the
existing element must be copied to the new storage (which results in a shallow
copy of the malloc'd memory, and leads to a double free).

When you clear the vector it doesn't need to be resized, so no element is
copied, so no shallow copy.

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