Wednesday, 4 March 2015

Smart pointers -Shared_ptr , week_ptr, unique_ptr and auto_ptr

Smart pointers -Shared_ptr , week_ptr, unique_ptr and auto_ptr with examples:

Smart pointers are the abstract data types , these can handle the major memory problem that occurred in the code. These smart pointers track the memory that they are pointing to. As we know that the majority of bugs happens in the code because of bad memory management. Smart pointers will take care of these type of problems and they deallocate the memory automatically. Smart points eliminates the dangling pointers.

Now look into the each type of pointer.


This pointer is available in #include

A unique_ptr is a container for a raw pointer, which the unique_ptr is said to own. A unique_ptr explicitly prevents copying of its contained pointer (as would happen with normal assignment), but the std::move function can be used to transfer ownership of the contained pointer to another unique_ptr. A unique_ptr cannot be copied because its copy constructor and assignment operators are explicitly deleted.

Declaration of the unique_ptr:

std::unique_ptr<int> ptr1(new int(5)); 

we can get the compile error for the below line of the code
std::unique_ptr<int> ptr2 = ptr1;  

Here using move function we can Transfers ownership. ptr3 now owns the memory and ptr1 is rendered invalid.
std::unique_ptr<int> ptr3 = std::move(ptr1);  

To delete the pointer


when we try to delete the memory using ptr1, it will not do anything


The copy constructor and assignment operators of auto_ptr do not actually copy the stored pointer. Instead, they transfer it, leaving the previous auto_ptr object empty.

This was one way to implement strict ownership, so that only one auto_ptr object could own the pointer at any given time. This means that auto_ptr should not be used where copy semantics are needed.


A shared_ptr is a container for a raw pointer. This is available in the header file
#include <memory>

It manages reference counted  ownership of its contained pointer in cooperation with all copies of the shared_ptr.

The referenced object will be destroyed when and only when all copies of the shared_ptr have been destroyed.

Let us look for the implementation of the shared_ptr

std::shared_ptr<int> shptr1(new int(5));
std::shared_ptr<int> shptr2 = shptr1;

The above two pointers own the memory

 When we want to destroy the memory we can implement


in the above case the memory still exists, due to shptr2.

We can reset the pointer shptr2 to delete the total memory


the above statement deletes the memory, since no one else owns the memory.

Because the implementation of shared_ptr uses reference counting, circular references are potentially a problem. A circular shared_ptr chain can be broken by changing the code so that one of the references is a weak_ptr.

A weak_ptr is a container for a raw pointer. It is also available in the header file calles
 #include <memory>

It is created as a copy of a shared_ptr.

The existence of weak_ptr copies of a shared_ptr have no effect on the shared_ptr or its other copies. After all copies of a shared_ptr have been destroyed, all weak_ptr copies become empty.

Let us see the implementation of the week_ptr:
std::shared_ptr<int> shptr1(new int(5)); 

Shptr1 owens the memory
 std::weak_ptr<int> wkptr1 = Shptr1;  

 //Now p1 and p2 own the memory.

  std::shared_ptr<int> shptr2 = wkptr1.lock();

 if(shptr2)  {
  // your statements that are using with the shptr2

After the above if condition shptr2 is destroyed. Memory is owned by shptr1.

Memory is deleted.

Let us see another scenario:

std::shared_ptr<int> shptr3 = wkptr1.lock();

In the above case the memory is gone, so we get an empty shared_ptr.


An empty weak_ptr object does not point to any resources and has no control block. Its member function lock returns an empty shared_ptr object.

No comments: