effect of free()

Henry Spencer henry at utzoo.uucp
Fri Sep 15 02:35:34 AEST 1989


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



More information about the Comp.lang.c mailing list