简体   繁体   中英

Is unique_ptr constructor initializes the raw pointer and also unique_ptr destructor deletes the associated raw pointer?

First time I am using smart pointers in my project. While using unique_ptr, I got some doubts regarding unique_ptr and raw pointer combination. And the way unique_ptr works internally.

Could some one please explain/answer based on my understanding as mentioned below, so that I can go ahead and use the smart pointers.

Below is the example:

class A 
{ 
public: 
    void show() 
    { 
        cout<<"A::show()"<<endl; 
    } 
}; 

int main() 
{ 
    unique_ptr<A> p1 (new A); 

    p1 -> show(); 

    // returns the memory address of p1 
    cout << p1.get(); 

   retrun 0;

}

From the above example,

  1. When creating unique_ptr object "p1" we are providing raw pointer. Internally, unique_ptr constructor will initialize the unique_ptr with the raw pointer. Is my understanding correct?

  2. As per the unique_ptr definition, "The pointer is exclusively owned by one object or a resource".

    Based on the above statement, in our scenario, "raw pointer" is exclusively owned by the unique_ptr object "p1". Am I correct?

  3. And also after the statement, cout << p1.get(); (In the above sample program) as it is going out of scope, internally, the destructor of the unique_ptr called and it deletes the associated raw pointer. Is my understanding correct?

  4. Finally, once deletes the associated raw pointer is the unique_ptr object will become empty?

When creating unique_ptr object "p1" we are providing raw pointer. Internally, unique_ptr constructor will initialize the unique_ptr with the raw pointer. Is my understanding correct?

Yes. The unique pointer will hold the same address.

As per the unique_ptr definition, "The pointer is exclusively owned by one object or a resource".

Based on the above statement, in our scenario, "raw pointer" is exclusively owned by the unique_ptr object "p1". Am I correct?

Yes. The only reference, the one that owns the resource and will free it, is the unique pointer. Note however that it's not the pointer that's owned, but the object it points at. The unique_ptr didn't take ownership of the raw pointer, it took ownership of the object (the resource) that is at the address the raw pointer provided.

And also after the statement, cout << p1.get(); (In the above sample program) as it is going out of scope, internally, the destructor of the unique_ptr called and it deletes the associated raw pointer. Is my understanding correct?

Yes. The unique ptr will cause the deletion of its internal raw pointer when it goes out of scope.

Finally, once deletes the associated raw pointer is the unique_ptr object will become empty?

Doesn't have to. Since the deletion happens when the unique_ptr object itself is being destroyed, there is no real need to "empty" it. It's about to go out of existence anyway, so its value is immaterial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM