Newsgroups: comp.lang.c
Path: utzoo!henry
From: henry@utzoo.uucp (Henry Spencer)
Subject: Re: effect of free()
Message-ID: <1989Sep14.163534.26982@utzoo.uucp>
Organization: U of Toronto Zoology
References: <319@cubmol.BIO.COLUMBIA.EDU> <3756@buengc.BU.EDU> <1989Aug17.005548.745@twwells.com> <16022@vail.ICO.ISC.COM> <248@seti.inria.fr> <gZ3LTWu00Xc8M0Nm0l@andrew.cmu.edu> <11053@smoke.BRL.MIL> <YZ3p34a00XcA04hUZt@andrew.cmu.edu>
Date: Thu, 14 Sep 89 16:35:34 GMT

In article <YZ3p34a00XcA04hUZt@andrew.cmu.edu> jdr+@andrew.cmu.edu (Jeff Rosenfeld) writes:
>union pi {
>    char *ptr;
>    unsigned long num;
>} x;
>
>x.ptr = malloc(AMOUNT);
>if (x.ptr != NULL) free(x.ptr);
>foo(x.num);

Section 3.3.2.3, Oct 88 draft:  "With one [irrelevant] exception, if a
member of a union object is accessed after a value has been stored in a
different member of the object, the behavior is implementation-defined."

>This is perfectly legal code...

It will compile, but an implementation is entirely within its rights
to generate a core dump when you try to execute it.

Incidentally, why do you assume that x.num has the same contents as x.ptr?
There are a number of machines on which they aren't the same size.
-- 
V7 /bin/mail source: 554 lines.|     Henry Spencer at U of Toronto Zoology
1989 X.400 specs: 2200+ pages. | uunet!attcan!utzoo!henry henry@zoo.toronto.edu
