Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!brutus.cs.uiuc.edu!apple!bloom-beacon!think!barmar From: barmar@think.COM (Barry Margolin) Newsgroups: comp.lang.c Subject: Re: effect of free() Message-ID: <29032@news.Think.COM> Date: 8 Sep 89 16:48:33 GMT References: <319@cubmol.BIO.COLUMBIA.EDU> <3756@buengc.BU.EDU> <10971@smoke.BRL.MIL> <2054@munnari.oz.au> <247@ssp1.idca.tds.philips.nl> Sender: news@Think.COM Organization: Thinking Machines Corporation, Cambridge MA, USA Lines: 54 In article <247@ssp1.idca.tds.philips.nl> dolf@idca.tds.PHILIPS.nl (Dolf Grunbauer) writes: >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). It *could* raise a trap. The standard doesn't define the behavior, so anything is permitted. >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 ? An implementation that does this would not conform to the standard. If some hardware does this, then the C implementation would have to generate code to work around it. I think such an implementation would be extremely unlikely, though -- it's likely that the address has to be loaded into a register in the process of making the system call that frees the segment, so it would be an extremely bad idea to fault in this case. >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) ? As I said, the effect is undefined. A conforming program should never try to use a pointer variable if it doesn't know whether it is valid. >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 :-) ? There's no portable way to check whether a pointer is valid. Even if there were, comparing it to the null pointer probably wouldn't be the way. >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)) ? I doubt that it's valid. The cast of the pointer variable might be implemented using address operations. Also, even if ptr were null, there's no guarantee that ((long)ptr) == 0) is true; the result of casting a pointer to an integer is implementation-dependent. >A final question: how valid is this discussion ? Is there any CPU (commercial >available) which has this sort of address checking ? I answered this in my posting last night. The Honeywell DPS-88 checks addresses during loads. Barry Margolin Thinking Machines Corp. barmar@think.com {uunet,harvard}!think!barmar