I was experimenting with braced initialisation and specifically using value initialisation.
To my understanding int x {}
uses value initialisation to set the value of x to 0.
So I then tried this with a std::shared_ptr
and a trivial inner class Foo
:
std::shared_ptr<Foo> sharedFoo {}
and tried to call a function from sharedFoo. I was slightly surprised to see this resulted in a segmentation fault.
My intuition had made me think that this would be value initialised, using the default constructor for Foo
. However, I then realised that sharedFoo has not actually had any memory allocated to it, using either new Foo
or std::make_shared
. Is my reasoning correct here. Is my reasoning correct here? I was hoping to default initialise a shared_ptr using simple braces.
My full code for reference is below:
#include <memory>
#include <iostream>
class Foo
{
public:
int getValue() const { return value; }
private:
int value = 0;
};
int main()
{
int i {};
std::shared_ptr<Foo> sharedFoo { };
std::cout << i << std::endl;
std::cout << sharedFoo->getValue() << std::endl; //SEGFAULT
return 0;
}
For reference, my question is not the following: std::shared_ptr initialization: make_shared<Foo>() vs shared_ptr<T>(new Foo)
Really, the core of the question is why int i {}
performs a value initialisation of i
, but std::shared_ptr<Foo> sharedFoo {}
does a default initialisation of sharedFoo and doesn't value initialise the contained pointer.