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


take ordinary struct (or class) plain old data types , objects members. note there no default constructor defined.

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

now if declare instance f on stack , attempt print contents:

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

the result garbage data printed x, y, , z. , string default initialized empty. as expected.

if create instance of shared_ptr<foo> using make_shared , print:

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

then, x, y, , z 0. makes appear shared_ptr performs default initialization (zero init) on each member after object instance constructed. @ least that's observe visual studio's compiler.

is standard c++? or necessary have explicit constructor or explicit ={} statement after instantiation guarantee zero-init behavior across compilers?

if see e.g. this std::make_shared reference see that

the object constructed if expression ::new (pv) t(std::forward<args>(args)...), pv internal void* pointer storage suitable hold object of type t.

that means std::make_shared<foo>() new foo(). is, value initializes structure leads zeroing of non-class member variables.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -