mardi 5 mars 2019

In one case shared_ptr cause runtime error, in another doesn't

Why

struct X{};

X x;
X *q = &x;
std::shared_ptr<X> p(&x);

causes error and

X x;
X *q = &x;
std::shared_ptr<X> p0 = std::make_shared<X>(x);

doesn't cause runtime error?

I read that in the first case I have "two different pointers, pointing to the same data and one of them is shared"

But I think that there is the same situation in the second case?

runtime error

free(): invalid pointer

Aucun commentaire:

Enregistrer un commentaire