On Thu, 2024-06-06 at 10:19 -0700, Andrii Nakryiko wrote: [...] > > Some other test, would expect that struct fields would be in some > > particular order, while GCC decides it would benefit from reordering > > struct fields. For passing those tests I need to disable GCC > > optimization that would make this reordering. > > However reordering of the struct fields is a perfectly valid > > Nope, it's not. > > As mentioned, struct layout is effectively an ABI, so the compiler > cannot just reorder it. Lots and lots of things would be broken if > this was true for C programs. I'll chime in as well :) Could you please show a few examples when GCC does reordering? As Alexei and Andrii point out in general C language standard does not allow reordering for fields, e.g. here is a wording from section 6.7.2.1, paragraph 17 of "WG 14/N 3088, Programming languages — C": > Within a structure object, the non-bit-field members and the units > in which bit-fields reside have addresses that increase in the order > in which they are declared. A pointer to a structure object, > suitably converted, points to its initial member (or if that member > is a bit-field, then to the unit in which it resides), and vice > versa. There may be unnamed padding within a structure object, but > not at its beginning. So, I'm curious what's happening.