[net.lang] levelheight: automatic "nil pointer" handling: ENUF!

jans@mako.UUCP (Jan Steinman) (01/31/85)

<6881@watdaisy.UUCP> ndiamond@watdaisy.UUCP (Norman Diamond) writes, quotes:
>>>For instance, it is possible to statically guarantee that nil pointers are
>>>never dereferenced...
>>
>> Ada supports this:  [quoted from Booch]
>
>The "answer" has nothing to do with the problem...

All right, enough of you have told me I didn't understatnd the question.
I don't think is is practical (possible?) to insure that null pointers will
never be dereferenced at compile time.

>The first netter's suggested problem was to diagnose programs that
>dereference nil pointers.  This... can be done dynamically.

Ada has made this fairly easy to do.  Simply put at the bottom of your code:

	exception
		when CONSTRAINT_ERROR =>
			put("You probably dereferenced a pointer, jerk!);
			dumpPointers();	-- user supplied procedure.
	end;

Unfortunately, the CONSTRAINT_ERROR exception also detects other things, such
as array bounding, range constraints, etc.  Perhaps a cleaner, more defensive
posture is to isolate the exception handler to blocks which are capable of
dereferencing a pointer, and somewhere near the top declare a special 
handler:

	-- In a high-level module:
	NULL_POINTER : exception;
	...
	exception
		when NULL_POINTER =>
			put("Now we know for sure you barfed a pointer!");
	end;

	-- This block accesses an object through a pointer:
	begin
		-- dereference a pointer here...
	exception
		when CONSTRAINT_ERROR =>
			raise NULL_POINTER;
	end;
	-- continue with non-pointer statements which may raise
	-- CONSTRAINT_ERROR, they will not effect NULL_POINTER exception.

Somewhat clumsy, and a lot more keystrokes than "C", but it will do
dynamic null dereferencing checking.  I imagine there are even ways of making
"C" do this. :-)
-- 
:::::: Jan Steinman		Box 1000, MS 61-161	(w)503/685-2843 ::::::
:::::: tektronix!tekecs!jans	Wilsonville, OR 97070	(h)503/657-7703 ::::::