On Jan 22, 2006, Richard Sandiford <[EMAIL PROTECTED]> wrote: > Ian Lance Taylor <ian@airs.com> writes: >> [EMAIL PROTECTED] (Kai Henningsen) writes:
>>> void test(void) { union { int i; double d; } u; >>> int *ip; double *dp; int ii; double dd; >>> ip = &u.i; *ip = 15; ii = *ip; dp = &u.d; *dp = 1.5; dd = *dp; >>> printf("ii=%d dd=%f\n", ii, dd); } >>> So you're saying this function is not valid? >> >> That function is valid. > I'm going to regret this, but... I don't follow. How is Kai's testcase > different from: > int ii; double dd; void foo (int *ip, double *dp) { > *ip = 15; ii = *ip; *dp = 1.5; dd = *dp; } > void test (void) { union { int i; double d; } u; > foo (&u.i, &u.d); } > ? foo() can write *dp before reading *ip. I don't think it is any different. GCC's exception for unions only applies if the object is accessed using the union type. So they are indeed equivalent. The scary thing is that I don't think they actually violate the ISO C strict aliasing rules, but they might still be mis-optimized by GCC, assuming I understand correctly what GCC does. -- Alexandre Oliva http://www.lsd.ic.unicamp.br/~oliva/ Red Hat Compiler Engineer [EMAIL PROTECTED], gcc.gnu.org} Free Software Evangelist [EMAIL PROTECTED], gnu.org}