Referencing NULL pointers

Chris Torek chris at mimsy.UUCP
Wed Jul 19 02:59:42 AEST 1989


In article <1759 at cadillac.CAD.MCC.COM> ned at pebbles.cad.mcc.com (CME
Ned Nowotny) writes:
>The argument over whether or not a C compiler should allow dereferencing
>of 0 (NULL) or any other numeric constant purporting to be an address is
>unrelated to the question of portability.

Actually, it is related, other than in the obvious way (the result of

	*(sometype *)(some integer expression)

is never portable).  The peculiar thing is that, even on machines
where location 0 is addressible and has something there (e.g., restart
and interrupt vectors), the expression

	*(char *)0

might get you something from somewhere other than location 0.  You
may have to write

	int zero = 0;
	...
	*(char *)zero

to get at location 0.  A compiler is free to compile

	int *p = 0;
	...
	if (p != 0) { ... }
	return;

as

		link	a6,#-4
		mov.l	#0xf8000003,a6@(-4)
		...
		cmp.l	#0xf8000003,a6@(-4)
		jeq	L91
		...
	L91:	unlk	a6
		rts

Here, a nil pointer to int is being represented with the value
0xf8000003.  For debugging purposes, if nothing else, it can be handy
to have your compiler represent every different nil pointer constant as
a different bit pattern.

>"We have ways to make you scream." - Intel advertisement in the June 1989 DDJ.

(They certainly do. :-) )
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.unix.wizards mailing list