Friday, 22 June 2018

auto_ptr, unique_ptr, shared_ptr and weak_ptr

auto_ptr is a smart pointer that manages an object obtained via new expression and deletes that object when auto_ptr itself is destroyed.


nt main()
{
    // p1 is an auto_ptr of type A
    auto_ptr p1(new A);
  
   
// returns the memory address of p1
    cout << p1.get() << endl;
 
    // copy constructor called, this makes p1 empty.
    auto_ptr p2(p1);
    p2 -> show();
 
    // p1 is empty now
    cout << p1.get() << endl;
 
    // p1 gets copied in p2
    cout<< p2.get() << endl;
}


two pointers of same type can’t point to the same resource at the same time.
As shown in below program, copying or assigning of pointers changes the ownership i.e. source pointer has to give ownership to the destination pointer.
Image_1
============================
unique_ptr std::unique_ptr was developed in C++11 as a replacement for std::auto_ptr.
unique_ptr is a new facility with a similar functionality, 

So, when using unique_ptr there can only be at most one unique_ptr at any one
resource and when that unique_ptr is destroyed, the resource is automatically claim.
unique_ptr ptr1 (new A);

 // Error: can't copy unique_ptr
 unique_ptr ptr2 = ptr1;    
But, unique_ptr can be moved using the new move semantics i.e. using std::move() function to transfer ownership of the contained pointer to another unique_ptr.
// Works, resource now stored in ptr2
unique_ptr ptr2 = move(ptr1); 


Use unique_ptr when you want to have single ownership(Exclusive) of resource. Only one unique_ptr can point to one resource.
=======================================
A shared_ptr is a container for raw pointers. It is a reference counting ownership model
Reference Counting: It is a technique of storing the number of references, pointers or handles to a resource such as an object
    shared_ptr p1 (new A);
   
    shared_ptr p2 (p1);
   
 
    // Returns the number of shared_ptr objects
    //referring to the same managed object.
    cout << p1.use_count() << endl;   ///2
    cout << p2.use_count() << endl;////2
 
    // Relinquishes ownership of p1 on the object
    //and pointer becomes NULL
    p1.reset();
    cout << p1.get() << endl; ///0
    cout << p2.use_count() << endl;///1
    cout << p2.get() << endl; ////0x1c2345


==========================
Cyclic Dependency (Problems with shared_ptr): Let’s consider a scenario where we have two classes A and B, both have pointers to other classes. So, it’s always be like A is pointing to B and B is pointing to A. Hence, use_count will never reach zero and they never get deleted.
Image_2
This is the reason we use weak pointers(weak_ptr) as they are not reference counted. So, the class in which weak_ptr is declared doesn’t have strong hold of it i.e. the ownership isn’t shared, but they can have access to these objects.
Image_3
So, in case of shared_ptr because of cyclic dependency use_count never reaches zero which is prevented using weak_ptr.



























No comments:

Post a Comment