RFC: Handling realloc(0)

Maciej W. Rozycki macro@orcam.me.uk
Fri Apr 30 19:46:07 GMT 2021


On Fri, 30 Apr 2021, Howard Chu wrote:

> >     bfd_realloc (ptr, 0)           will return ptr
> 
> Shouldn't this return NULL? a standard realloc(ptr, 0) == free(ptr).

 Nope, the standard has[1]:

"If the size of the space requested is zero, the behavior is 
implementation-defined: either a null pointer is returned, or the behavior 
is as if the size were some nonzero value, except that the returned 
pointer shall not be used to access an object."

so this approach is perfectly fine.

References:

[1] "Programming languages -- C", INTERNATIONAL STANDARD, ISO/IEC 9899,
    Second edition, 1999-12-01, Section 7.20.3 "Memory management 
    functions", p.312

  Maciej


More information about the Binutils mailing list