4 ms·
Looking at the ISO 9899:2018 spec, a few sections appear to be relevant. This one I wasn't familiar with, and I believe it absolves the comment I initially rep
by david-gpu 2y ago
Looking at the ISO 9899:2018 spec, a few sections appear to be relevant.
This one I wasn't familiar with, and I believe it absolves the comment I initially replied to in this thread.
> 6.5.2.3.6 states:
> One special guarantee is made in order to simplify the use of unions: if a union contains several
structures that share a common initial sequence (see below), and if the union object currently contains
one of these structures, it is permitted to inspect the common initial part of any of them anywhere
that a declaration of the completed type of the union is visible
Outside of that special case, the general semantics of unions are specified as follows. I think that means you are right?
> 6.2.6.1.7 states
> 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
- flohofwoe 2y agoThere is also a footnote: 99) If the member used to read the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called "type punning"). This might be a trap representation. ...it all makes sense until that last sentence, maybe that's for really weird bit casts like trying to reinterpret an integer as float. FWIW the only place I used union type punning was in an Z80 emulator for the 16-bit register pairs, but that's no longer necessary, since modern compilers will optimize two consequitive byte reads into a single word read: https://www.godbolt.org/z/or7Woc4TP https://www.godbolt.org/z/or7Woc4TP