On Sun, 21 Apr 2024, Thorsten Glaser wrote: > Michael Forney dixit: > > >Something like > > > >#if __STDC_VERSION__ >= 201112L > >/* use _Alignas */ > >#elif defined(__cplusplus) && !defined(__GNUC__) > >/* use alignas */ > >#else > >/* use __attribute__((__aligned__(N))) */ > >#end > > Something I noticed recently while doing m68k alignment work: > > The C++ alignas is UB if the specified alignment is smaller > than what the structure would normally have, so adding cautious > alignments can explode in one’s face. ☹ Not only is this really > stupid, but makes it not generally usable, too. > > GCC’s attribute, in contrast, (without __packed__) just gets > ignored for those cases. This is inaccurate: you can use the attribute to decrease alignment of scalar types (but you do need __packed__ when attaching the attribute to a struct). As I recall, GCC documentation used to be misleading or wrong about this, but the current wording is fairly clear to me: When used on a struct, or struct member, the aligned attribute can only increase the alignment; in order to decrease it, the packed attribute must be specified as well. When used as part of a typedef, the aligned attribute can both increase and decrease alignment, and specifying the packed attribute generates a warning. https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html Alexander