effect of free()

Jeff Rosenfeld jdr+ at andrew.cmu.edu
Thu Sep 14 17:18:28 AEST 1989


> Excerpts from netnews.comp.lang.c: 13-Sep-89 Re: effect of free() Doug
> Gwyn at smoke.BRL.MIL (1384)

> the very mention of "loading into a general-purpose
> register" implies a certain limited class of architectures.  The
> discussion was about what MIGHT legally occur in a C implementation
> on SOME architecture. 

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. 

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. 

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.

			- Jeff (fearing days of strongly-typed assemblers).



More information about the Comp.lang.c mailing list