Referencing NULL pointers

Doug Gwyn gwyn at smoke.BRL.MIL
Fri Jul 21 09:06:31 AEST 1989


In article <1796 at cadillac.CAD.MCC.COM> ned%cad at MCC.COM (CME Ned Nowotny) writes:
>There are byte-addressable systems where important data may reside at
>address 0.

I think Chris and I know that.  What we're saying is, don't attempt to
use what appears like an integer constant "0" to construct an access to
such a location.  Instead, try something like
	#define	word_at(loc)	((int*)(sizeof(int)+(loc))[-1]
which will not run afoul of C's null pointer convention.  Of course,
there may be some C implementations (maybe "Safe C")  that perform
run-time dereferencing checks, in which case it would be possible that
there is no valid way to access address zero directly from C.  I doubt
that you'd be using such an implementation if you were munging address
zero, however.

>Other languages do not care whether 0 is a valid address or not.

I have no doubt that it would have been preferable to have designed
an explicit "nil" keyword into C.  For one thing, it would have avoided
these endless discussions about dereferencing null pointers!  However,
as with numerous other "warts", we're stuck with it and merely need to
learn how to live with it.  For the vast majority of applications it
should not pose a problem.



More information about the Comp.lang.c mailing list