Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 5 Jul 2006 12:04:30 -0400
From:      John Baldwin <jhb@freebsd.org>
To:        freebsd-hackers@freebsd.org
Cc:        Pat Lashley <patl+freebsd@volant.org>, Matthias Andree <matthias.andree@gmx.de>, Johannes Weiner <hnazfoo@googlemail.com>
Subject:   Re: Return value of malloc(0)
Message-ID:  <200607051204.31577.jhb@freebsd.org>
In-Reply-To: <m3hd21g1ss.fsf@merlin.emma.line.org>
References:  <20060628181045.GA54915@curry.mchp.siemens.de> <417C9B11412FF8C17A1AD483@Zelazny> <m3hd21g1ss.fsf@merlin.emma.line.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Saturday 01 July 2006 04:35, Matthias Andree wrote:
> Pat Lashley <patl+freebsd@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. :)

-- 
John Baldwin



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200607051204.31577.jhb>