On Sat, 2014-09-13 at 08:23 +0100, Andrew Haley wrote: > On 12/09/14 23:58, haynberg@xxxxxxxxx wrote: > >> Firstly, char types alias everything. > > (I'm not sure why you wrote that?) > > Because the specification says so. 6.3.2.3, Pointers, in C9X. > > > I know if you cast to char type, > > it can, but I'm going from a char type. > > > >> Secondly, even if you call memcpy(), a compiler doesn't have to do any > >> copies if it can prove that the union you're reading into doesn't > >> escape. > > If the compiler can optimize away memcpy, why memcpy into a union, why > > not just to the type in question? Or is memcpy into a union special? > > You can copy the bytes from one object to another, and it has the > same effect. I can't guarantee it generates the same code as a > union in all cases. > > > In other words, if I write: > > > > msg p; > > memcpy(&p, get_bytes(), sizeof p); // assume the size OK > > if (p.i) > > // ... > > > > Can the memcpy be optimized away, making it similar to the cast version > > (but not undefined)? I ran into a similar thing a while ago. See also https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59478 for some experiments. Probably the optimizations will look differently on other targets than SH. On strict alignment targets there's also this memcpy issue https://gcc.gnu.org/bugzilla/show_bug.cgi?id=50417 which might be related. Cheers, Oleg