C++ Logo

std-proposals

Advanced search

Re: [std-proposals] realloc(3)

From: Alejandro Colomar <une+cxx_std-proposals_at_[hidden]>
Date: Thu, 30 Oct 2025 18:25:04 +0100
Hi Jonathan,

On Thu, Oct 30, 2025 at 03:59:42PM +0000, Jonathan Wakely via Std-Proposals wrote:
> All that,

I'll address that separately.

> and it also just does too many things for a single API.
>
> It can allocate new memory, free old memory, reallocate existing memory,
> ... why even bother having malloc and free, realloc can do it all!

This is a misconception based on the attempt of the committee and some
implementations (dating back to the original sinner, SysVr3) to push on
disallowing zero-sized objects.

See also: <https://nabijaczleweli.xyz/content/blogn_t/017-malloc0.html>.

realloc(NULL,) does indeed allocate, and malloc(3) is technically
redundant, as much as printf(3) is redundant with fprintf(stdout,).
But malloc(3) is still a nice commodity wrapper, as is printf(3).

However, realloc(p,0)-as-free(3) is a false generalization that does not
work, and pushing for it has resulted in the UB for realloc(p,0) we have
today. The proposal I've written --and which the Austing Group has
OK'd-- removes this realloc(p,0)-as-free(3) idea. realloc(p,0) should
always (and if all goes well, will always) allocate a new pointer, so
free(3) needs to exist.


Have a lovely day!
Alex

-- 
<https://www.alejandro-colomar.es>
Use port 80 (that is, <...:80/>).

Received on 2025-10-30 17:25:12