references to dereferenced null pointers

Chris Torek chris at mimsy.umd.edu
Fri Mar 16 13:28:49 AEST 1990


In article <1136 at philmtl.philips.ca> ray at philmtl.philips.ca
(Raymond Dunn) writes:
>... In any program, you are free to use *any* value you like to represent
>*your* value of the illegal pointer.

No, not really.

>As an example you could define:
>
>#define NOPTR(type) ((type *)-1)

The conversion from any integral constant other than zero to any
pointer type is up to the implementation.  Most often, this means
that for any

	#define XXX ((mytype *)some_int_constant)

you find two problems: the constant you picked happens to be equal
to a valid pointer, and/or: the constant you picked causes the run
time system to phone your mother and leave a nasty message on her
answering machine.  (Well, not really. :-)  But something like
`NOPTR(char)' may cause a run-time fault---a `bus error (core dumped)'
sort of error.)

Incidentally, the reason for the latter is that the system is allowed
to trap as soon as an invalid pointer is examined or created.

Again, the only solidly defined conversion from integral constant to
pointer is for zero: the integral constant zero becomes, in a pointer
context, a nil pointer of the appropriate type.  (In addition, every
nil pointer is in principle different, except for nil pointer to char
vs. nil pointer to void.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.std.c mailing list