[Scummvm-devel] "free(0)" is valid C, and so is "delete 0;"

Max Horn max at quendi.de
Sun Aug 1 15:40:06 CEST 2004


Am 02.08.2004 um 01:34 schrieb David Given:

> Max Horn wrote:
> [...]
>> FYI, this change is not doing anything "more correct", it produces  
>> identical code. The C standard specifically defines that free(0) does 
>>  nothing. So there is no reason to checking a value for being 
>> non-NULL  before freeing it.
>
> Back when I was a C novice, I was firmly taught that malloc(0) and 
> free(0) should be avoided in portable code because regardless of what 
> the standards said, different vendors' C libraries tended to be 
> creatively broken and you were never quite sure if it would work or 
> not.
>
> Admittedly, this was on a Sun SLC where sometimes I'd have to compile 
> a program with a combination Sun's K&R C compiler, Sun's ANSI C 
> compiler, and a very early version of gcc --- whichever made that 
> particular module compile --- and then link the results together and 
> hope it worked.
>
> Can I now assume that such hideous breakage is now a thing of the 
> past, and assume that malloc(0) and free(0) do sensible things?

Yes.

> *Are* there still C libraries around that have this bug?
>
If I learned one thing, it's "Never say never". But I can say that none 
of our target platforms has such a hideous buggy standard library -- 
else our existing code wouldn't work (as it already relies on this).


Bye,

Max





More information about the Scummvm-devel mailing list