Skip to content

Instantly share code, notes, and snippets.

@nlguillemot
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlguillemot/6332c70c9e6cfb242ed7 to your computer and use it in GitHub Desktop.
Save nlguillemot/6332c70c9e6cfb242ed7 to your computer and use it in GitHub Desktop.
value initializing unions inside a struct
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
#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