
Patrick Horgan wrote:
THAT's what the warning is trying to tell you, that the optimizer is going to do things that you don't like.
It's trying but not succeeding. The code is fine for two reasons, each of them sufficient. One, it stores an object of type T and then accesses an object of that same type T. Two, accessing an object via a pointer to char/unsigned char is allowed.
Scary, isn't it? Of course you could use -fno-strict-aliasing to get the right output, but the generated code won't be as good. A better way to accomplish the same thing without the warning's or the incorrect output is to define swaphalves like this:
uint32_t swaphalves(uint32_t a) { union swapem{ uint32_t a; uint16_t b[2]; };
...
This follows the rules...
No it doesn't. Writing to one union member and reading another is undefined. You have to use memcpy if you care about the rules.