13

Take an ordinary struct (or class) with Plain Old Data types and objects as members. Note that there is no default constructor defined.

struct Foo
{
    int x;
    int y;
    double z;
    string str;
};

Now if I declare an instance f on the stack and attempt to print its contents:

{
    Foo f;
    std::cout << f.x << " " << f.y << " " << f.z << f.str << std::endl;
}

The result is garbage data printed for x, y, and z. And the string is default initialized to be empty. As expected.

If I create an instance of a shared_ptr<Foo> using make_shared and print:

{
    shared_ptr<Foo> spFoo = make_shared<Foo>();
    cout << spFoo->x << " " << spFoo->y << " " << spFoo->z << spFoo->str << endl;
}

Then, x, y, and z are all 0. Which makes it appear that shared_ptr performs a default initialization (zero init) on each member after the object instance is constructed. At least that's what I observe with Visual Studio's compiler.

Is this standard for C++? Or would it be necessary to have an explicit constructor or explicit ={} statement after instantiation to guarantee zero-init behavior across all compilers?

m7913d
  • 10,244
  • 7
  • 28
  • 56
selbie
  • 100,020
  • 15
  • 103
  • 173

2 Answers2

14

If you see e.g. this std::make_shared reference you will see that

The object is constructed as if by the expression ::new (pv) T(std::forward<Args>(args)...), where pv is an internal void* pointer to storage suitable to hold an object of type T.

That means std::make_shared<Foo>() basically does new Foo(). That is, it value initializes the structure which leads to the zeroing of the non-class member variables.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
8

To be more precise, std::make_shared employes value initialization syntax,

The object is constructed as if by the expression ::new (pv) T(std::forward<Args>(args)...)

For Foo with an empty initializer list, that means all its members with built-in type will be zero-initialized, and std::string will be default-initialized.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405