null pointers

D. Chadwick Gibbons chad at lakesys.UUCP
Mon Jul 24 20:08:29 AEST 1989


	I have seen so many contradictions of the defintion of null and most
of the constant discussion of it has caused my sensability of the defintion of
null to fly way out in left field.  Let me equate to you my current
understanding of the null pointer, and if anyone wishes, they may mail me a
different story.  I don't think the news group needs another rash of postings,
so let's stay away from that.

	As defined by K&R2 (A6.6, p. 198) null is

		"An integral constant expression with [the] value 0, or such
	an expression cast to type void * [which may be] converted, by a cast,
	by assignment, or by comparision, to a pointer of any type."

Which means to me that if you state a comparision such as "if (ptr == 0)" then
you are indeed checking for a null pointer in a valid way.  On the actual
symbolic constant of NULL, The Book says (p. 102):
		"The symbolic constant NULL is often used in place of zero, as
	mnemonic to indicate more clearly that this is a special value for a
	pointer."
Fine.  That makes sense.

	From what I have seen however, NULL may not be a number with a zero
bit pattern, since some implementations do not store zero as a zero bit
pattern (depending if the value in question is signed or unsigned.)  Thus,
zero and NULL may not equal each other in all implementations, yet _both_ may
be used to safely in comparision of a null pointer.  It appears that in
pre-ANSI C, this is not true, and often the symbol constant NULL requires
casting to the proper type to ensure proper conversion of alignment
restrictions.

	And then there is the problem of some architectures storing data at
address zero.  Occording to the defintions above - this does not matter.  It
is the compilers job to assume that the constant zero is for checking for a
null pointer, and if valid data can be at address zero that comparing a
pointer against zero is _not_ a comparision with that address.  Or so I have
extrapolated.

If you respond to this in way, shape, or form, try reading the whole thing
first, go grab something with caffiene in it, and then respond.
-- 
D. Chadwick Gibbons, chad at lakesys.lakesys.com, ...!uunet!marque!lakesys!chad



More information about the Comp.lang.c mailing list