Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!virtech!cpcahil From: cpcahil@virtech.UUCP (Conor P. Cahill) Newsgroups: comp.lang.c Subject: Re: effect of free() Message-ID: <1152@virtech.UUCP> Date: 14 Sep 89 16:38:54 GMT References: <319@cubmol.BIO.COLUMBIA.EDU> <3756@buengc.BU.EDU> Organization: Virtual Technologies Inc Lines: 58 In article , jdr+@andrew.cmu.edu (Jeff Rosenfeld) writes: > OK - then consider the following code fragment (no pointer/int flames, > please - they're not relevant here): > > union pi { char *ptr; unsigned long num; } x; > > x.ptr = malloc(AMOUNT); > if (x.ptr != NULL) free(x.ptr); > foo(x.num); > > This is perfectly legal code (despite that x.num contains nothing of > guaranteed usefulness) and any compiler that generates code that causes > a seg fault on the call to foo() has some serious problems. Yes a compiler should not generate a seg fault with the use of x.num because x.num is not a pointer and the compiler should not treat it as such. However, had you called foo() with x.ptr, the compiler would treat the value as a pointer and could possibly fail because the pointer does not have a valid address in it. > If you accept the previous statement, then it follows that any > architecture capable of supporting an implementation of C must be able > to perform a test on an arbitrary pointer. The test operation might be > implemented through GP registers or with memory operands or whatever. > But it HAS to be doable if the architecture can support C. I don't accept the previous statement so this whole paragraph is mute. > Of course, if the ANSI committee deems my example illegal then I guess > it's time to find another language-of-choice. In the meantime, I'd > appreciate if someone can prove by counter-example using any real or > imagined architecture that my claim is bogus. You're example is not illegal (although it is of little use). But you are not touching the "element" that is at an issue here: whether or not you can examine an invalid pointer. You did not examine a pointer. You examined an unsigned long. -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+