On Sun, Mar 21, 2010 at 01:04:44AM +0100, Stefan Reinauer wrote:
On 3/18/10 3:47 PM, Carl-Daniel Hailfinger wrote:
AFAIK (and I could be wrong) type punning is not only about alignment, but the C compiler is free to optimize the generated code in a way that may break your expectations if you're violating strict aliasing rules.
That's what I read over and over, too but I can't make much sense of it... what does it mean? What are we not supposed to expect? What is it about, more than alignment?
It's about pointer aliases. Let's say one had code like:
void myfunc(u16 *s, u32 *l) { printf("%d", *l); *s += 1; *l += 1; printf("%d", *l); }
Then gcc is free to assume that *s and *l don't point to the same memory location. Thus, myfunc((u16*)&myint, &myint) could give unexpected results.
The gcc warnings point out risky looking casts, but it is not exhaustive. Fixing all the warnings will not prevent all aliasing issues.
Also, I didn't think alignment made any real impact on embedded x86 - as I didn't think gcc would emit any code that required alignment.
-Kevin