effect of free()

Dolf Grunbauer dolf at idca.tds.PHILIPS.nl
Fri Sep 8 18:22:05 AEST 1989


In article <2054 at munnari.oz.au> ok at cs.mu.oz.au (Richard O'Keefe) writes:
>In article <10971 at smoke.BRL.MIL>, gwyn at smoke.BRL.MIL (Doug Gwyn) writes:
>In article <246 at ssp1.idca.tds.philips.nl> dolf at idca.tds.PHILIPS.nl (Dolf Grunbauer) writes:
>> -I agree with Dick, as the original statement is:
>> -   if (ptr == 0) ...
>It is perfectly true that loading a (formerly valid, now invalid) address
>into an address register might cause a trap.  BUT there is no reason why
>a compiler has to translate "if (ptr == 0)" by loading ptr into an
>address register.  Presumably, whatever bit pattern represents the NULL
>pointer on such machines may _also_ cause a trap if loaded into an
>address register (several versions of UNIX, for example, invalidate the
>first page of a virtual memory precisely to catch dereferencing NULL),
>so a compiler has to be prepared to handle "ptr = 0;" without loading an
>invalid address into such a register.  It also has to be prepared to handle
>"ptr2 = ptr1;" and "if (ptr2 == ptr1)" where ptr1 may be NULL (and thus
>might cause a trap if loaded into an address register).  So what's so
>special about other (pointer values that may not be dereferenced)?

What I don't understand from the answer of Richard is that according to
him the compiler takes care of these cases so where is our problem ?
Let me explain what I mean. The original posting asked why:
   ptr = malloc(1);
   free(ptr);
   if (ptr == 0)
is illegal or could cause (portability) problems. Now Richard states that
the compiler sovles the problem. Hence there is no problem. On the other
hand there are some people who state that the "if (ptr == 0)" is still
illegal (which I interpret now as: the compiler did not take care of this
case and the generated code raises some sort of a trap).

What about the case when ptr is already in a register
(i.e. definition of ptr: register char *ptr) ? Will there be an address trap
right after the free as some address register now holds an invalid address ?
By the way: what is the effect of the address trap: does the "if (ptr == 0)"
always evaluate to FALSE or is there a signal (SIGSEGV) ?
If so: how can I check in my program whether ptr is still valid (after all
that's why we had the "if (ptr == 0)" in the first place :-) ?
If "if (ptr == 0)" cases some sort of a trap or is illegal, is the expression
"if ((long)ptr == 0)" legal, as ptr will now be loaded in a data register
instead of an address register (assuming: sizeof(cahr *) == sizeof(long)) ?

A final question: how valid is this discussion ? Is there any CPU (commercial
available) which has this sort of address checking ? As far as I have read
from the net only such CPU's are hypothetical, c.f.
Doug Gwyn: (1) cause an exception on some reasonable architectures. 
           (2) There aren't many common architectures where this would be a
               problem, but it can happen.
Martin Jourdan: Imagine a segmented memory architecture with protections ...
-- 
Dolf Grunbauer          Tel: +31 55 432764  Internet dolf at idca.tds.philips.nl
Philips Telecommunication and Data Systems  UUCP ....!mcvax!philapd!dolf
Dept. SSP, P.O. Box 245, 7300 AE Apeldoorn, The Netherlands



More information about the Comp.lang.c mailing list