When I executed the code below,
#include <memory>
#include <iostream>
class AAA
{
public:
AAA(void)
{
std::cout<<"Ctor"<<std::endl;
}
void show()
{
std::cout<<"Foo"<<std::endl;
}
};
int main(void)
{
std::shared_ptr<AAA> a; // uninitialized
a->show();
return 0;
}
The result comes out like this without any failure:
Foo
But I guess shouldn't a seg fault or something occur at the time of calling a->show()
?
And when I checked further, if there was a member variable, then a seg fault occurred.
#include <memory>
#include <iostream>
class AAA
{
public:
AAA(void)
{
std::cout<<"Ctor"<<std::endl;
}
void show()
{
std::cout<<"Foo"<<std::endl;
std::cout<<mData;
}
private:
int mData;
};
int main(void)
{
std::shared_ptr<AAA> a;
a->show();
return 0;
}
The result comes out like this:
Foo
Segmentation fault (core dumped)
What is the reason for these differences? In particular, I wonder why the seg fault did not occur in the first case.