A zero pointer is not a null pointer
Posted Jul 24, 2009 20:38 UTC (Fri) by giraffedata
In reply to: A zero pointer is not a null pointer
Parent article: Fun with NULL pointers, part 1
It would be possible to keep 0x0 for the null pointer while respecting the C99 standard: the compiler would need to put in a couple of extra instructions for every pointer comparison making sure that 0x0 != P for any value of P.
Do we know Gcc doesn't do this? Seems like it would have to, to be C99 compliant.
Optionally, it could also put in a check before every pointer dereference making sure the pointer is not 0x0 and causing a SIGSEGV if it is (since the memory layout can no longer be relied on to guarantee that).
But then how would you represent a pointer to a data structure that resides at address 0? A pointer should be able to do that.
It would of course be unrealistically expensive on typical machines to represent a pointer with anything but a simple address, but pointer comparisons are rare enough that a few extra instructions for them seems worthwhile to maintain the null pointer concept.
Regardless of how the compiler chooses to represent pointers (null or otherwise), the optimization in question is logically sound. C99 says a dereference of a null pointer causes undefined behavior, so either a) tun is non-null and !tun must be false or b) tun is null and !tun can be anything, including false.
to post comments)