I want to access the shared ptr, which is in the union, although a segmentation error occurs:
struct union_tmp
{
union_tmp()
{}
~union_tmp()
{}
union
{
int a;
std::shared_ptr<std::vector<int>> ptr;
};
};
int main()
{
union_tmp b;
std::shared_ptr<std::vector<int>> tmp(new std::vector<int>);
b.ptr = tmp;
return 0;
}
What is the cause of the error and how to avoid it?
source
share