Creating pointer with all bits 0 (was: Referencing NULL pointers)

Richard Tobin richard at aiai.ed.ac.uk
Mon Sep 11 23:41:29 AEST 1989


]]Question: Is an implementation whose null pointer is the same as the
]]result of some integer-to-pointer cast conforming?  Apparently not,
]]since 3.2.2.3 states, "... a null pointer is guaranteed to compare
]]unequal to a pointer to any object or function."
]
]This looks like a bit of a problem all right.  Can it still be fixed
]by editorial change?  For example, "... any legally created object or
]function" or "... any C object or function."

I would have thought the right solution to this was for there to be no
guarantee that the result of an arbitrary integer-to-pointer cast be
an "object or function".  Indeed, I believe this is the case: 3.3.4 says
"An arbitrary integer may be converted to a pointer.  The result is
implementation-defined."

In a "reasonable" implementation, casting a geuine pointer to an integer
and back again will result in the same pointer, so it certainly won't
compare equal to zero.  Casting other integers to pointers should only
be done when you know enough about the machine and compiler you are using.

[Please, no flames about casting pointers to integers.  I know it's not
guaranteed to do anything useful, but it does on all the machines I care
about, and has completely reasonable uses.]

-- Richard
-- 
Richard Tobin,                       JANET: R.Tobin at uk.ac.ed             
AI Applications Institute,           ARPA:  R.Tobin%uk.ac.ed at nsfnet-relay.ac.uk
Edinburgh University.                UUCP:  ...!ukc!ed.ac.uk!R.Tobin



More information about the Comp.lang.c mailing list