effect of free()

Richard O'Keefe ok at cs.mu.oz.au
Fri Sep 8 13:12:16 AEST 1989


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)?



More information about the Comp.lang.c mailing list