#include <iostream>
union A {
int i;
double d;
char c;
}; // the whole union occupies max(sizeof(int), sizeof(double), sizeof(char))
int main()
{
A a = { 43 }; // initializes the first member, a.i is now the active member
// at this point, reading from a.d or a.c is UB
std::cout << "a.i = " << a.i << std::endl;
a.c = 'a'; // a.c is now the active member
// at this point, reading from i or d is UB but most compilers define this
std::cout << "a.i = " << a.i << std::endl; // 97 most likely
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdW5pb24gQSB7CiAgICBpbnQgaTsKICAgIGRvdWJsZSBkOwogICAgY2hhciBjOwp9OyAvLyB0aGUgd2hvbGUgdW5pb24gb2NjdXBpZXMgbWF4KHNpemVvZihpbnQpLCBzaXplb2YoZG91YmxlKSwgc2l6ZW9mKGNoYXIpKQoKaW50IG1haW4oKQp7CiAgICBBIGEgPSB7IDQzIH07IC8vIGluaXRpYWxpemVzIHRoZSBmaXJzdCBtZW1iZXIsIGEuaSBpcyBub3cgdGhlIGFjdGl2ZSBtZW1iZXIKICAgIC8vIGF0IHRoaXMgcG9pbnQsIHJlYWRpbmcgZnJvbSBhLmQgb3IgYS5jIGlzIFVCCiAgICBzdGQ6OmNvdXQgPDwgImEuaSA9ICIgPDwgYS5pIDw8IHN0ZDo6ZW5kbDsKICAgIGEuYyA9ICdhJzsgLy8gYS5jIGlzIG5vdyB0aGUgYWN0aXZlIG1lbWJlcgogICAgLy8gYXQgdGhpcyBwb2ludCwgcmVhZGluZyBmcm9tIGkgb3IgZCBpcyBVQiBidXQgbW9zdCBjb21waWxlcnMgZGVmaW5lIHRoaXMKICAgIHN0ZDo6Y291dCA8PCAiYS5pID0gIiA8PCBhLmkgPDwgc3RkOjplbmRsOyAvLyA5NyBtb3N0IGxpa2VseQogICAgcmV0dXJuIDA7Cn0=