Re: Why does this code break strict-aliasing rules?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jonathan Lennox wrote:

Am I missing something, or is gcc mis-compiling correct code?
gcc is correct.

You put a value into union derived_union* u and then read a value out of ((struct base*)u)

That is a violation of strict aliasing.

Annoying as it is, the compiler is free to assume the value you put in is unrelated to the value you read, so the value you put in u is never used, so the optimizer can remove that operation.

Why do you want to cast the union pointer rather than just use it?

I understand you are relying on the fact that the base part of each of derived1 and derived2 will be the first thing in the union, so without strict aliasing your code would be correct.  But as long as you rely on that placement it is simpler and more correct to just use
u->d1.base.a = a;



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux