I have a basic question about shared pointers, in the next example code:
int main() {
std::shared_ptr<int> sp; // empty
std::shared_ptr<int> sp2;
sp.reset (new int);
sp2.reset (new int);
*sp=10;
*sp2=400;
sp2=sp;
std::shared_ptr<int> sp3;
sp3=sp1;
//what happens with the int of value 400?
//more code
}
what happens with the int of value 400? is this a memory leak? this is a basic example but if instead of int the pointers were pointing to a big class, then it would be problematic if there was a memory leak, and more if sp2 is reasigned more times. thanks!
After your edits, no there's no leaks. The memory allocated for the int
with the value 400
will simply be deleted for you in the assignment.