I'm trying to experience with smart_ptrs and so I was trying to return a unique_ptr from a function :
unique_ptr<int> foo()
{
int x = 100;
unique_ptr<int> up(new int);
cout << "value stored in up : " << *up << endl;
cout << "get : " << up.get() << endl;
up.reset(&x);
cout << "value stored in up : " << *up << endl;
cout << "get : " << up.get() << endl;
return up;
}
int main()
{
auto up = foo();
cout << "value stored in up : " << *up << endl;
cout << "get : " << up.get() << endl;
return 0;
}
As I expected up in main is pointing to the same address up in foo was last pointing at, and just before the print statement in main up holds 100 as I expected it too, the problem is right before I print the value stored in up, it gets reset and so I print something else followed up by a failed debug assertion and I'm clueless to why it happens..