Referencing NULL pointers

Chuck Karish karish at forel.stanford.edu
Wed Jul 12 03:29:50 AEST 1989


In article <32UP02Eg3d=801 at amdahl.uts.amdahl.com> vohra at amdahl.uts.amdahl.com
(KC) wrote:
>In article <1891 at auspex.auspex.com> guy at auspex.auspex.com (Guy Harris) writes:
[ It's not valid to dereference a NULL pointer ]

>Try a rewrite: "x= ((struct somestruct *)0)->somefield".

>It can be convenient to make the first page (or whatever unit)
>inaccessible to cause errors for structures that are smaller
>than that unit.  Maybe Sunos does this...

>Anyway, it is the operating system that makes the action that the
>process takes valid or invalid.

Neither the draft C standard nor Kernighan and Ritchie define the
format of an executing program.  Accessing an arbitrarily-chosen
address in memory is thus completely non-portable; there's no way to
predict whether 0 is a valid address.  The `0' value for NULL is a
token with a special meaning, not an address.

Whether non-portable usages are `valid' depends on the way the program
is to be used.  If the program is to have a long life or should be
usable on more than one architechure, don't dereference NULL pointers.

	Chuck Karish		{decwrl,hpda}!mindcrf!karish
	(415) 493-7277		karish at forel.stanford.edu



More information about the Comp.unix.wizards mailing list