ANSI C

Wayne A. Christopher faustus at ucbcad.BERKELEY.EDU
Tue Aug 19 04:27:27 AEST 1986


If there is a compiler that defines NULL as something other than an
all-0 bit pattern, I certainly hope that "if (!p)" will do the right
thing, otherwise most existing code won't work.  But this should never be
a real problem, since all the loader has to do is guarantee that
nothing valid will ever have an address that corresponds to the
conversion of integer 0 to a pointer.  There are no architectural
considerations involved -- if there are tag bits, etc, just let them be
all 0 -- the pointer should never be referenced anyway, so who cares
what the tag bits are.

In article <86900017 at haddock>, karl at haddock writes:
> > "Fixing" it the other way, by having any integral expression with the value
> > 0 convert to a null pointer, would ... surprise some code that does want to
> > grab a pointer value [as int] and then stuff the value back into the
> > pointer, if a pointer value of all zero bits is a valid pointer.

You can always use a union, or cast a pointer to the needed value -- these
tricks are used for things like dissecting floating point numbers.

> Hmm, there's a similar problem without the full-check conversion.  If the
> constant zero is converted into a null pointer constant, and zero is a valid
> address, and something of interest is at that absolute address, how do I
> reference it? 

If there is something of interest at location 0 then you should complain to
whomever put it there.  It's a very good idea to make null pointer references
cause a fault -- this catches lots of nasty bugs.  If this isn't the case
on your machine, you can still use the tricks mentioned above.

	Wayne



More information about the Comp.lang.c mailing list