Return value of malloc(0)
jhb at freebsd.org
Wed Jul 5 19:35:02 UTC 2006
On Saturday 01 July 2006 04:35, Matthias Andree wrote:
> Pat Lashley <patl+freebsd at volant.org> writes:
> > BUT, that said, the safest and most portable coding practice would be:
> > // The C standard does not require malloc(0) to return NULL;
> > // but whatever it returns MUST NOT be dereferenced.
> > ptr = ( size == 0 ) ? NULL : malloc( size ) ;
> Safest (avoiding null derefence) would instead be:
> ptr = malloc(size ? size : 1);
> BTW: // is not a valid C89 comment, but a GCC-ism.
It's valid in C99 though. :)
More information about the freebsd-hackers