Alignment seems to be your problem, check out the pragma pack . The struct / union names simply refer to the allocated memory block, if st_t.a is shifted in the structure, adding more members, your tide will fail, but if it remains the first member, it will work, since all members of the union point to the same address. as the union itself.
See C ++ Standard Section 9.2.17-21: “A pointer to a standard layout structure object properly converted using reinterpret_cast indicates its initial member (or if this element is a bit field, and then to the unit in which it located) and vice versa. "
See also section 9.5 Unions: 1. In a union, no more than one non-static data element can be active at any time, that is, the value of no more than one of the non-static data elements can be stored in the union at any time. [Note. To simplify the use of joins, there is one special guarantee: if the union of the standard layout contains several standard layout structures that have a common initial sequence (9.2), and if the object of this type of standard layout contains one of the standard layout structures, it is allowed to check the general initial sequence of any of the elements standard layout structures, see 9.2. - end note). The size of the union is sufficient to contain the largest of its non-static data. Each member of the non-static data is distributed as if it were the only member of the structure. "
source share