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

David Given dg at cowlark.com
Sun Aug 1 15:34:11 CEST 2004


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? *Are* there 
still C libraries around that have this bug?

-- 
David Given
dg at cowlark.com




More information about the Scummvm-devel mailing list