Javatpoint Logo
Javatpoint Logo

Smart Pointer

Pointers are used to access resources outside of the programme, such as heap memory. As a result, pointers are used to access heap memory (if anything is created inside heap memory). We simply use a copy of any external resource when accessing it. We simply change it in the copied version if we make any changes. However, if we use a pointer to the resource, we can change the original resource.

Issues with Normal Pointers

Check out the code below.

C++ Code

Function fun generates a pointer pointing to the Rectangle object. Rectangle is made up of two integers: length and breadth. Because p is a local variable, it will be destroyed when the function fun ends. However, because we forgot to use delete p; at the end of the function, the memory it consumed will not be deallocated. This means that the memory will not be available for use by other resources. However, we no longer require the variable, but rather the memory.

Fun is called in an infinite loop in function main. That means it will continue to generate p. It will allocate more memory but will not free it because we did not deallocate it. The unused memory cannot be used again. This results in a memory leak. Because of this, the entire heap memory may become useless. Smart Pointer, a feature of C++11, provides a solution to this problem.

Smart Pointers are being introduced.

Unconsciously, not dealinglocating a pointer causes a memory leak, which may result in a programme crash. Garbage Collection Mechanisms are used in the languages Java and C# to intelligently deallocate unused memory so that it can be used again. The programmer does not have to be concerned about memory leaks. Smart Pointer is a mechanism developed by C++11. When the object is destroyed, the memory is also released. As a result, we don't need to delete it because Smart Pointer will handle it.

A Smart Pointer is a pointer wrapper class that has operators like * and -> overloaded. The smart pointer class's objects resemble regular pointers. However, unlike Normal Pointers, it has the ability to deallocate and free destroyed object memory.

Take a class that has a pointer, a destructor, and overloaded operators like * and ->. Because the destructor is automatically called when an object exits scope, the dynamically allocated memory would be deleted (or reference count can be decremented). Consider the SmartPtr class shown below.

C++ Code

Output:

20

This only applies to int. So we'll need to make a Smart Pointer for each object? No, Template, there is a solution. As you can see in the code below, T can be of any type. Learn more about Template by clicking here.

C++ Code

Output:

20

Note: Smart pointers can also be used to manage resources such as file handles or network sockets.

Smart Pointer Types

1. unique ptr

Only one pointer is stored in unique ptr. By removing the current object from the pointer, we can assign a different object. Take note of the code below. First, the unique_pointer is pointing to P1. However, we remove P1 and replace it with P2, so the pointer now points to P2.

Smart Pointer

C++ Code

Output:

50
50

2. shared_ptr

Using shared ptr, multiple pointers can point to the same object at the same time, and it will keep a reference counter using the use count() method.

Smart Pointer

C++ Code

Output:

50
50
50
2

3. weak_ptr

It is very similar to shared ptr, except that it does not keep a Reference Counter. A pointer will not have a stronghold on the object in this case. The reason for this is that if pointers are holding the object while requesting other objects, they may form a deadlock.

Smart Pointer





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA