Last active
August 29, 2015 14:22
-
-
Save nlguillemot/6332c70c9e6cfb242ed7 to your computer and use it in GitHub Desktop.
value initializing unions inside a struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Compiled with /EHsc /nologo /W4 | |
main.cpp | |
Compilation successful! | |
Total compilation time: 109ms | |
0 0 0 0 0 0 0 0 0 0 | |
50790056 50790132 50790220 49536592 -1801320171 -2 49555713 49555747 49683692 6 | |
Total execution time: 515ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
union U | |
{ | |
int x; | |
float y; | |
}; | |
struct A | |
{ | |
U us[10]{}; | |
}; | |
int main() | |
{ | |
// array of union: | |
// value init seems to zero out | |
U us[10]{}; | |
for (U u : us) | |
{ | |
cout << u.x << " "; | |
} | |
cout << "\n"; | |
// struct of array of union: | |
// the array member is value initialized, | |
// yet contains garbage initially. | |
// Compiler bug? Spec bug? C++ shenanigans? | |
A a; | |
for (U u : a.us) | |
{ | |
cout << u.x << " "; | |
} | |
cout << "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment