Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.csd.uwm.edu!gem.mps.ohio-state.edu!ginosko!uunet!mcsun!hp4nl!philapd!ssp1!dolf From: dolf@idca.tds.PHILIPS.nl (Dolf Grunbauer) Newsgroups: comp.lang.c Subject: Re: effect of free() Message-ID: <247@ssp1.idca.tds.philips.nl> Date: 8 Sep 89 08:22:05 GMT References: <319@cubmol.BIO.COLUMBIA.EDU> <3756@buengc.BU.EDU> <10971@smoke.BRL.MIL> <2054@munnari.oz.au> Organization: Philips Telecommunication and Data Systems, The Netherlands Lines: 52 In article <2054@munnari.oz.au> ok@cs.mu.oz.au (Richard O'Keefe) writes: >In article <10971@smoke.BRL.MIL>, gwyn@smoke.BRL.MIL (Doug Gwyn) writes: >In article <246@ssp1.idca.tds.philips.nl> dolf@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@idca.tds.philips.nl Philips Telecommunication and Data Systems UUCP ....!mcvax!philapd!dolf Dept. SSP, P.O. Box 245, 7300 AE Apeldoorn, The Netherlands