[comp.lang.c] Dereferencing zero

dant@tekla.TEK.COM (Dan Tilque;1893;92-789;LP=A;60aC) (01/12/88)

Jim Shankland writes:
>
>	Code that dereferences 0 is buggy.  If I
>write such buggy code, I want to hear about it immediately by getting
>an access violation when I do the dereference.  I DON'T want my application
>to continue running, possibly doing horrible things, because the language
>implementor tried to do me a favor by covering up my bug.

While *0 is bad and should be appropriately flagged, sometimes you need to
reference a structure that begins at location zero.  For example:

	struct system_vectors	*svp;

	svp = 0;
	(svp->whatever_vector)();

Of course, this only works in supervisor mode on most machines, but
sometimes you have to do it.

---
Dan Tilque
dant@tekla.tek.com  or dant@tekla.UUCP

rsalz@bbn.com (Rich Salz) (01/13/88)

>	struct system_vectors	*svp;
>	svp = 0;
>	(svp->whatever_vector)();

Isn't this code guaranteed by ANSI, K&R, H&S, (and Guy Harris :-) to be
illegal?  The minute you assign zero to a pointer you have essentially
delcared that pointer invalid and illegal.  I can understand why you might
need/want to do this in system-specific code, but I don't understand how
it's any more legal than
	strcpy(my_buf, 0) is.

Replies to me and I'll summarize for the net.
	/r$
-- 
For comp.sources.unix stuff, mail to sources@uunet.uu.net.

gwyn@brl-smoke.ARPA (Doug Gwyn ) (01/13/88)

In article <2953@zeus.TEK.COM> dant@tekla.UUCP (Dan Tilque) writes:
>	struct system_vectors	*svp;
>	svp = 0;
>	(svp->whatever_vector)();

If you really have to do something like this, odds are your compiler
will support it.  However, be aware that you have really, according
to the proposed C standard, made `svp' a null pointer by the assignment,
and an implementation may represent null pointers specially.  There are
many reasons for that that I don't want to get into.  Just remember that
the C standard does not guarantee that you can ever use 0 as the address
of an object (quite the contrary), but some implementations may permit it.