int main(int argc, char *argv[])
{
auto sp = std::make_shared<int>();
auto p = sp.get();
delete p; //here
std::cout << *sp << std::endl;
return 0;
}
I was hoping that the object managed by the shared_ptr sp
will be deleted by the statement commented "here" but the object remains intact and is printed in the output statement.
- Is this not working because I didn't create the object managed by the
shared_ptr
explicitly with thenew
keyword (because everynew
statement must have a correspondingdelete
statement and therefore everydelete
statement, an earlier correspondingnew
keyword?)? - And does this mean that pointed objects managed by
shared_ptr
s, created from themake_shared
function are destroyed only by theshared_ptr
s themselves?