I am facing an issue with value initialization mixed with aggregate initialization. So far, I tried to rely on doing all my initializations like this:
auto var = Type{};
(Yes, I am aware of brace-initializer ctor vs default ctor pitfall. So no comments about that please!)
I expect that this would properly "zero out" or init the memory of var.
But in VS 2013 Update 2, I see this:
#include <string>
#include <iostream>
using namespace std;
struct B
{
double g[10];
std::string str;
};
struct C
{
double g[10];
};
struct A
{
double a[3];
double b = 0;
double d;
struct B b_stuff;
struct C c_stuff;
A() : b_stuff{}, c_stuff{} {}
};
int main()
{
auto a = A{};
double big[50] = {};
for(auto b : a.b_stuff.g) { cout << b << " "; }
cout << endl;
cout << endl;
for(auto b : a.c_stuff.g) { cout << b << " "; }
cout << endl;
cout << endl;
for (auto b : big) { cout << b << " "; }
return 0;
}
The output is this:
-9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596e+061
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
With GCC 4.7.2:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
I read this but I don't see reason for this non-zeroing behaviour:
http://en.cppreference.com/w/cpp/language/value_initialization http://en.cppreference.com/w/cpp/language/aggregate_initialization
So, is VS 2013 buggy? Why doesn't it zero out the a.b_stuff.g array?
b_stuff
(I have missed that line when writting my answer). – Manu343726