references to dereferenced null pointers

Wayne Throop throopw at sheol.UUCP
Sun Mar 18 08:28:18 AEST 1990


> From: guy at auspex.auspex.com (Guy Harris)
>> I'm confused, is a non zero NULL pointer valid or not?
> The confusion stems from confusion over the meaning of "non-zero".

While what Guy says in explanation is certainly correct, I think
that the confusion is over the meaning of "NULL pointer", not
non-zero.  That is, Guy explained things thoroughly, but the basis
of the problem is the distinction between properties of a name
of a thing and properties of the thing itself.

In the phrase "NULL pointer" in C, there are TWO levels of naming
going on, either one of which could have the property "zeroness".

Or let me put it this way.

In C, the name of the nil pointer is called "NULL".

But that's only what the name is CALLED, you see.

The NAME of the nil pointer is "0".

The nil pointer itself can have any bit pattern it pleases. 



The above sequence of statements explains
   1) why NULL should only be used as a pointer (this is a convention).
      It shouldn't be used to mean ascii NUL
   2) why the only proper definition of the macro NULL is the string "0".
      (or, actually, any way to spell the constant zero in C)
      (though, granted, the advent of ANSI C means that the best
       definition of NULL is arguably the string "((void*)0)" and
       variants on this theme.  Nevertheless "0" remains proper.)
   3) in just what way C's nil pointer may have a non-zero bit pattern.

It omits explanations of why one should never (in K&R1 C) pass NULL
as an argument without a cast to a specific pointer type.

It also omits explanation of the fact that there isn't really one
nil pointer, or (necessarily) one nil pointer bit pattern.  (This
is due to the fact that all pointer values in C have specific
types.)

So, to conclude, "NULL", in C should never have a "non-zero" definition.

Any particular nil pointer value (eg, one named ((void*)0)) can have any
bit pattern the implementor of a C language system chooses. 

--
Wayne Throop <backbone>!mcnc!rti!sheol!throopw or sheol!throopw at rti.rti.org



More information about the Comp.lang.c mailing list