On Wed, 19 Jun 2024 at 11:57, Alexander Monakov <amona...@ispras.ru> wrote: > > Hello, > > I vaguely remember there was a recent, maybe within last two months, > discussion > about semantics of union initialization where sizeof(first member) is less > than > sizeof(union). The question was whether it's okay to initialize just that > first > member and leave garbage bits in the other, larger, members of the union, like > in this example: > > union A { > char a; > long : 0; > }; > > void fn(void *); > > void my(void) > { > union A a = { 0 }; > fn(&a); > } > > (except in my example there's no other named member, but I think the example > in that discussion was less contrived) > > Perhaps somebody remembers where it was (I'm thinking Bugzilla) and could > point > me to it? My attempts to search for it aren't turning anything up so far.
Somebody asked about this internally at Red Hat recently, and I responded with this quote from C17 6.2.6.1 p7: "When a value is stored in a member of an object of union type, the bytes of the object representation that do not correspond to that member but do correspond to other members take unspecified values. " This looks related too: https://discourse.llvm.org/t/union-initialization-and-aliasing-clang-18-seems-to-miscompile-musl/77724/3 They don't seem to have found the quote above though. I think it got reported to GCC's bugzilla too, I'll see if I can find it again. > If someone knows what semantics GCC implements, that also would be welcome. GCC seems to initialize the trailing bits, unnecessarily.