Return value of malloc(0)
Matthias Andree
matthias.andree at gmx.de
Sat Jul 1 09:07:11 UTC 2006
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.
--
Matthias Andree
More information about the freebsd-hackers
mailing list