0
votes

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?

1
You are right, is a MSVC bug. You are correctly value initializing b_stuff (I have missed that line when writting my answer).Manu343726
Huh, "good". I started to panic/hope that I missed something obvious. If this is really a bug in MSVC then that is bad news...feheren.fekete
Try to use in-class value initialization, its allowed since C++11, but I don't know if the Microsoft folks implemented it: coliru.stacked-crooked.com/a/e1e6d42289ce54eeManu343726

1 Answers

2
votes

Visual C++ has a long and storied history of value initialization bugs. I believe Bug 746973 is the one you have stumbled across here.