What machines core dump on deref of NULL?

Christopher R Volpe volpe at underdog.crd.ge.com
Fri Jul 6 23:05:21 AEST 1990


In article <418 at minya.UUCP>, jc at minya.UUCP (John Chambers) writes:
> The trouble with this statement is that I've never seen a C compiler
> that implements it.  On extant processors, it is simple to prove that
> it can't be implemented.  If you examine any of the current commercial
> processors (68xxx, 8xx86, SPARC, MIPS, PDP11/VAX, etc.), you quickly
> learn that all of them have the property that there is no bit pattern
> that is guaranteed to cause a fault when used as an address. True,
> you can use the memory-management hardware to intercept attempts to
> reference ranges of addresses, but this is a different issue.  The
> memory-referencing hardware has no bit pattern that a compiler can 
> use as "null" value, with the guarantee that its use as an address
> will cause an interrupt under all circumstances.  All bit patterns
> are legal (byte) addresses on these machines.

Noone ever said that dereferencing a NULL pointer is guaranteed to
produce any kind of fault whatsoever. The only thing that is guaranteed
is that sticking a '&' in front of any object won't yield a NULL pointer. 
That includes storage for variables and functions reserved by the compiler
as well as malloced storage.

> 
> If I were designing my own processor, I'd probably try to make address 
> zero illegal, since that would catch so many bugs during early testing.  
> But I'm not, and I can't.  Given hardware that says that such-and-such 
> is stored at address zero, you either write code that references address 
> zero, or you hand the job over to someone else. 

Noone ever said that a NULL pointer means address zero.

> BTW, perhaps this should be asked in comp.lang.c (though I recall it
> being discussed a few years back, with much flamage but few answers);
> can anyone show how one would portably code a statement that assigns
> the value zero (not null) to a pointer?  If I am faced with hardware 
> with a given structure in low memory, it'd help if I could declare:
> 	struct lowmem {
> 		...
> 	} *lowmem = 0;
> and be guaranteed that it will point to the right place.  It'd also
> be nice to be able to do the assignment at run time if necessary. 
> As so many people have pointed out, the above code could legally be
> implemented as any illegal value; what I need is a way to guarantee
> that it will be zero, regardless of what null is and whether zero 
> is a legal address at the moment.

Here's how to get a zero bit pattern into a pointer variable:

type *ptr_to_type;
int i;

i=0; /* the integer zero, i.e. all zero bits */

ptr_to_type = *((type *) &i);

Portable? Well, only on machines where sizeof(int) == sizeof(type *). 
On machines with pointers bigger than ints and longs, you could use a 
union with one member being the pointer and the other member being
a sufficiently large array of ints and then just assign zero to all the
ints. Why bother trying to make it completely portable? Any application
that needs to reference address zero is pretty machine dependent already
anyway.

Chris Volpe
G.E. Corporate R&D
volpecr at crd.ge.com



More information about the Comp.unix.wizards mailing list