Referencing NULL pointers

Doug Gwyn gwyn at smoke.BRL.MIL
Mon Aug 28 13:57:42 AEST 1989


In article <1796 at cadillac.CAD.MCC.COM> ned%cad at MCC.COM (CME Ned Nowotny) writes:
>In article <10556 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
>>There better not be.  C guarantees that valid object addresses compare
>>unequal to null pointers, and since a null pointer constant is written
>>as "0" in C source code, you cannot obtain a valid object address by
>>casting 0 to the object pointer type.
>While Chris Torek and you have made good arguments against attempting
>to cast a 0 to a valid pointer, I believe you are losing sight of
>the distinction between a null pointer represented by the numeric
>constant 0 and the address 0 in a given environment.

No, in fact I directly addressed the idea of forcing an integral 0
"address" to point at an object.  This has nothing to do with whether
or not you can obtain useful data from "address" 0 by dereferencing
such a pointer.  The compiler is entirely at liberty to IMMEDIATELY
turn (whatever*)0 into an internal form such as (&__nullity), where
__nullity is part of the C library.  If you were able to dereference
such a "0-valued" pointer, in fact you'd access __nullity, not address 0.
Therefore you'd better not have anything at address 0 that really needs
to be accessed from C code.

>whether there are cases in which valid data resides at location zero in some
>systems, including implementations of UNIX, and the question of how to access
>it.

Original UNIX implementations inserted a "shim" at I-space virtual
address 0 to prevent useful data from being allocated there, just
to guarantee that null pointers were always distinct from pointers
to valid objects.



More information about the Comp.lang.c mailing list