Undefined behaviour in a small code snippet

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 Hello,

The program at the bottom prints
1
2
1
2

Does the code contain undefined behaviour in C++17 (note that the structs
have common initial sequence )?

Łukasz


#include<iostream>

using namespace std;

struct A {
	int first;
	int second;
};

struct B {
	int first;
	int second;
};

struct Test {
	union {
		A a;
		B b;
	};
};

int main() {
	Test t;
	t.a.first = 1;
	t.b.second = 2;
	cout << t.a.first << '\n';
	cout << t.a.second << '\n';
	cout << t.b.first << '\n';
	cout << t.b.second << '\n';
	return 0;
}




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux