BUG: realloc(p,0) should be consistent with malloc(0)

Wilco Dijkstra Wilco.Dijkstra@arm.com
Mon Jun 16 22:50:56 GMT 2025


Hi Alejandro,

> I'm working on that.  I have a proposal for mandating that malloc(0),
> but I can't present it until realloc(p, 0) is fixed.  

Presumably you mean that malloc (0) is no longer implementation defined
and defined to return a non-NULL pointer if it succeeds?

I would say that feels like the wrong way around. Firstly you need malloc (0)
to be non-NULL, and only then can realloc (p, 0) be well defined.

Otherwise when realloc (p, 0) returns NULL, you can't differentiate between
the malloc/free failing and malloc/free being successful (since both return
NULL and we can't rely on errno being set).

So do all implementations (including alternative allocators like jemalloc etc)
already return a non-NULL malloc (0)?

Cheers,
Wilco




More information about the Libc-alpha mailing list