Invalid Pointers (was Re: Referencing NULL pointers)

Steve Summit scs at adam.pika.mit.edu
Mon Jul 17 12:00:49 AEST 1989


In article <4348 at eos.UUCP> jbm at eos.UUCP (Jeffrey Mulligan) writes:
>It has been pointed out that there should be no assumptions
>about what addresses are valid; is there any way to get
>a guaranteed INVALID address?
>I commonly do this sort of thing:
>
>static struct foobar *fb1=NO_FOOBAR;
>
>So, the question is, how should NO_FOOBAR be defined?

NULL, or the equivalent 0, is precisely the right thing to use
in this situation.  The null pointer is, by definition,
guaranteed not to point to any object.

Using the intermediate NO_FOOBAR is still a good idea, by the
way, because it helps insulate the calling application from the
implementation details of the "foobar" code.  For instance, my
programs contain things like

	#include "graph.h"

	graphd gd = grph_alloc(...);

	if(gd == NOGRAPH)
		...

where graphd is a "graph descriptor" type defined with a typedef
in graph.h .  The caller is not supposed to know what kind of a
type graphd is (that's why it's a typedef).  If a graphd is a
pointer, NOGRAPH is 0, but if it's a small-integer descriptor
(like Unix file descriptors) NOGRAPH is -1.

>Sorry if this is the wrong newsgroup.

Actually, it is, and I'd redirect followups to comp.lang.c,
except that they're generally pretty tired of the NULL topic over
there.  (And yes, unadorned 0 is a guaranteed acceptable
#definition for NULL, so let's not start that discussion here.)

                                            Steve Summit
                                            scs at adam.pika.mit.edu



More information about the Comp.unix.wizards mailing list