Eg I'm pretty sure this works.
int foo = 51;
int* bar = &foo;
foo = 3;
So bar is still valid, and *bar == 3
.
What about if we say
std::unique_ptr<int> foo(new int(51)); // should probably use make_unique
int* bar = foo.get();
foo.reset(new int(3));
Am I guaranteed that *bar == 3
?
Or am I causing undefined behavior by continuing to reference bar?
unique_ptr::resetReplaces the managed object. 1) Given current_ptr , the pointer that was managed by *this, performs the following actions, in this order: Saves a copy of the current pointer old_ptr = current_ptr. Overwrites the current pointer with the argument current_ptr = ptr.
"Moving" transfers ownership to a new unique_ptr and resets the old unique_ptr .
That is, you should know that a unique_ptr will safely delete its underlying raw pointer once it goes out of scope.
One of the advantages of smart pointers is, that they ensure due to RAII, that the actual object is deleted. When using a raw pointer, you need to have a delete for every possible exit point, and still an exception will lead to a memory leak. Smart pointers will also free the memory if an exception occurs.
No, not at all. You will get undefined behaviour, as the raw pointer managed by the unique_ptr
is changed by the reset
. In fact, the unique_ptr
deletes the managed pointer and reallocates a new one, so your old pointer will keep pointing to the old address, which is no longer valid. Your code is equivalent to this:
#include <iostream>
int main()
{
int* managed = new int(51); // equivalent to unique_ptr(new int(51));
int* bar = managed; // equivalent of unique_ptr::get
delete managed; managed = new int(3); // equivalent to unique_ptr::reset(new int(3))
std::cout << *bar << std::endl; // Wrong!!! Undefined behaviour, bar is dangling!
}
Your first code snippet is indeed correct, since you are not changing any address there, you are just changing the variable the pointer points to, so of course dereferencing the pointer will get you the updated value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With