Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does make_shared do a default initialization (zero-init) for each member variable

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?

like image 288
selbie Avatar asked Jul 28 '17 06:07

selbie


People also ask

Are class members initialized to zero?

If T is scalar (arithmetic, pointer, enum), it is initialized from 0 ; if it's a class type, all base classes and data members are zero-initialized; if it's an array, each element is zero-initialized.

What does make_shared return?

Description. It constructs an object of type T passing args to its constructor, and returns an object of type shared_ptr that owns and stores a pointer to it.

What is the difference between make_shared and shared_ptr?

The difference is that std::make_shared performs one heap-allocation, whereas calling the std::shared_ptr constructor performs two.

Why is make_shared more efficient?

One reason is because make_shared allocates the reference count together with the object to be managed in the same block of memory.


1 Answers

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.

like image 182
Some programmer dude Avatar answered Sep 22 '22 12:09

Some programmer dude