https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86013

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jan Kratochvil from comment #0)
> std::vector::shrink_to_fit() when reducing the size it still calls
> new()+copy.
> It could use realloc() when the objects are trivially copyable resulting in
> no copy during size reduction.

In general it can't. It's only valid to do that when we know that the allocator
obtained memory from malloc, which we can't know for the default new_allocator
(users could have replaced operator new to get memory from somewhere else). It
would be OK for malloc_allocator only.

The only reliable way to get realloc-like behaviour is by extending the
allocator API to cover it.

> Maybe it could even always call realloc() for size reduction of any type of
> objects and just assert the returned pointer did not change.

That suggestion terrifies me.

Reply via email to