Pointer to unique_ptr – is this a loophole?

  • A+
Category:Languages

It seems an easy way to circumvent a unique_ptr is to use a pointer to the unique_ptr object. And that's not difficult. So using the unique_ptr is sort of a gentleman's agreement and not really super enforced?

#include <iostream> #include <memory> using namespace std;  class Box {   public:     int num; };  void update_box(unique_ptr<Box>* pu);  int main(){   unique_ptr<Box> b{new Box};   unique_ptr<Box>* u = &b;   update_box(u);   cout << b->num << endl; // Outputs 99.   return 0; }  void update_box(unique_ptr<Box>* pu) {   (*pu)->num = 99; } 

 


You actually put your real question into a comment:

My understanding of unique_ptr is that it should be used to ensure you only have one pointer to an object at any time.

No, this is wrong. You can easily do a simple:

std::unique_ptr<int> a(new int); int *b = a.get(); // b points to the same object as a 

The design of unique_ptr tries to ensure that you can only have one unique_ptr to an object. But even, it can ensure this only if you don't use naked pointers. With naked pointers, it is easy to sidestep this design:

std::unique_ptr<int> a(new int); std::unique_ptr<int> b(a.get()); 

Here, b points to the same object as a, so this program has undefined behavior (because the int object will be deleted twice).

unique_ptr's intent is to show ownership. As there can be only one unique_ptr which can point to an object (ignoring the "hack" I've presented earlier), unique_ptr owns the pointed object, and as unique_ptr's destructor is called, it will delete the pointed object.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: