Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 01 Jul 2006 10:35:47 +0200
From:      Matthias Andree <matthias.andree@gmx.de>
To:        Pat Lashley <patl+freebsd@volant.org>
Cc:        freebsd-hackers@freebsd.org, Johannes Weiner <hnazfoo@googlemail.com>
Subject:   Re: Return value of malloc(0)
Message-ID:  <m3hd21g1ss.fsf@merlin.emma.line.org>
In-Reply-To: <417C9B11412FF8C17A1AD483@Zelazny> (Pat Lashley's message of "Fri, 30 Jun 2006 12:30:13 -0400")
References:  <20060628181045.GA54915@curry.mchp.siemens.de> <20060629054222.GA92895@leiferikson.flosken.lan> <m3bqsceyf2.fsf@merlin.emma.line.org> <20060629162319.GA94921@leiferikson.flosken.lan> <m33bdnhnv7.fsf@merlin.emma.line.org> <20060630045937.GB97868@leiferikson.flosken.lan> <417C9B11412FF8C17A1AD483@Zelazny>

next in thread | previous in thread | raw e-mail | index | archive | help
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.

-- 
Matthias Andree



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