effect of free()

Conor P. Cahill cpcahil at virtech.UUCP
Fri Sep 15 02:38:54 AEST 1989


In article <YZ3p34a00XcA04hUZt at andrew.cmu.edu>, jdr+ at 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     |
+-----------------------------------------------------------------------+



More information about the Comp.lang.c mailing list