Yang Zhang wrote: > Andrew Haley wrote: >> Yang Zhang wrote: >>> Ian Lance Taylor wrote: >>>> Yang Zhang <yanghatespam@xxxxxxxxx> writes: >>>> >>>>> I expected a cast-align warning or something. >>>> If you use -Wcast-align you should get a warning for targets where the >>>> cast increases the required alignment. >>> Sorry for being unclear. I was trying to say that I had specified >>> -Wcast-align but got no such warning. Any hints? >> >> `-Wcast-align' >> Warn whenever a pointer is cast such that the required alignment >> of the target is increased. For example, warn if a `char *' is >> cast to an `int *' on machines where integers can only be accessed >> at two- or four-byte boundaries. >> >> but on x86 there is no such restriction. > > So __alignof__/alignment_of<> have nothing to do with the required > alignment of the type, but have instead to do with the gcc-determined > "default alignment" of the type? The alignment of a type is determined by the ABI; it's usually chosen to maximize performance. It may well be, as in this case, greater than the minimum alignment enforced by hardware. > Is there something analogous to __alignof__ that can tell me the > required alignment? (E.g., if I'm serializing data and trying to pack > together everything without padding...) I'm not aware of any such thing. The x86 is fairly unusual in that all the basic scalar types can be accessed at any alignment. However, if any object straddles a cache line boundary you can pay a high penalty, so it is very much worth aligning objects to make sure that doesn't happen. Andrew.