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

H. Peter Anvin hpa@zytor.com
Sat Jun 21 02:58:30 GMT 2025


On 2025-06-20 19:48, Alejandro Colomar wrote:
>>
>>>>
>>>> If a non-NULL pointer is returned, from either malloc or realloc, it needs
>>>> to be unique, but there is no requirement that it needs to be *accessible.*
>>>
>>> Nope, it doesn't need to be unique.  There's no requisite in the
>>> standard that the result of malloc(0) is unique.
>>>
>>
>> I think it is for C++, though.
> 
> Hmm, could be.  I was talking with Doug and we found it was removed in
> C99 (the requirement was present in C89).  I'll ask why it was changed
> some day, but I don't want to change the attention from realloc(3) from
> now.
> 
> Once we merge this, I'll start asking about uniqueness.
> 
> Also, Doug told me he has a use case for unique pointers, so I might
> write a paper for restoring that requirement, if we can.  But I'll do
> that after we finish with this.
> 

Either way, my main point was that the uniqueness requirement *can* be 
satisfied without backing it up with memory.

(And I have seen unique pointers being used as error sentinels, i.e. 
"null pointers with payload".)

	-hpa



More information about the Libc-alpha mailing list