Smart Pointers in C++
A smart pointer is a wrapper class around a raw pointer that automatically manages the lifetime of the object it points to.
- It ensures proper resource deallocation by automatically releasing the memory when the pointer goes out of scope, thus preventing memory leaks and dangling pointers.
- Smart pointers are defined in the
<memory>header and include types such asstd::unique_ptr,std::shared_ptr, andstd::weak_ptr.
#include <iostream>
using namespace std;
int main() {
// Infinite Loop
while (1) {
// Create a variable
// in heap memory using pointer
int* ptr = new int;
}
return 0;
}
In the above example, a memory leak occurs because memory is allocated but not freed after use.
Types of Smart Pointers
C++ libraries provide implementations of smart pointers in the following types:
auto_ptr
In C++, auto_ptr is a smart pointer that automatically manages the lifetime of a dynamically allocated object. It takes ownership of the object it points to, ensuring that the object is automatically deleted when the auto_ptr goes out of scope.

#include <iostream>
#include <memory>
using namespace std;
int main() {
// Pointer declaration
auto_ptr<int> ptr1(new int(10));
cout << *ptr1 << endl;
// Transfer ownership to
// pointer ptr2,
auto_ptr<int> ptr2 = ptr1;
cout << *ptr2;
return 0;
}
Output
10 10
Note: auto_ptr is deprecated after C++11 and it remove after C++ version 17.
unique_ptr
unique_ptr stores one pointer only at a time. We cannot copy unique_ptr, only transfer ownership of the object to another unique_ptr using the move() method.

#include <iostream>
#include <memory>
using namespace std;
class Rectangle {
int length;
int breadth;
public:
Rectangle(int l, int b) {
length = l;
breadth = b;
}
int area() { return length * breadth; }
};
int main() {
unique_ptr<Rectangle> P1(new Rectangle(10, 5));
cout << P1->area() << endl;
unique_ptr<Rectangle> P2;
// Copy the addres of P1 into p2
P2 = move(P1);
cout << P2->area();
return 0;
}
Output
50 50
shared_ptr
By using shared_ptr, more than one pointer can point to same object at a time, and it will maintain a reference counter using the use_count() method.

#include <iostream>
#include <memory>
using namespace std;
class Rectangle {
int length;
int breadth;
public:
Rectangle(int l, int b) {
length = l;
breadth = b;
}
int area() { return length * breadth; }
};
int main() {
shared_ptr<Rectangle> P1(new Rectangle(10, 5));
cout << P1->area() << endl;
shared_ptr<Rectangle> P2;
// P1 and P2 are pointing
// to same object
P2 = P1;
cout << P2->area() << endl;
cout << P1->area() << endl;
cout << P1.use_count();
return 0;
}
Output
50 50 50 2
weak_ptr
weak_ptr is a smart pointer that holds a non-owning reference to an object. It's much more similar to shared_ptr except it will not maintain a reference counter. In this case, a pointer will not have a stronghold on the object. The reason is to avoid the circular dependency created by two or more object pointing to each other using shared_ptr.

#include <iostream>
#include <memory>
using namespace std;
class Rectangle {
int length;
int breadth;
public:
Rectangle(int l, int b) {
length = l;
breadth = b;
}
int area() { return length * breadth; }
};
int main() {
// Create shared_ptr Smart Pointer
shared_ptr<Rectangle> P1(new Rectangle(10, 5));
// Created a weak_ptr smart pointer
weak_ptr<Rectangle> P2 (P1);
cout << P1->area() << endl;
// Returns the number of shared_ptr
// objects that manage the object
cout << P2.use_count();
return 0;
}
Output
50 1
Problems with Normal Pointers
- Memory Leaks: This occurs when memory is repeatedly allocated by a program but never freed. This leads to excessive memory consumption and eventually leads to a system crash.
- Wild Pointers: A pointer that never be initialize with valid object or address called wild pointer.
- Dangling Pointers: Assume there is a pointer that refers to memory which was deallocated earlier in the program, that pointer is called a dangling pointer.
Pointers vs Smart Pointers
Pointer | Smart Pointer |
|---|---|
| A pointer is a variable that maintains a memory address as well as data type information about that memory location. A pointer is a variable that points to something in memory. | Smart pointers, in simple words, are classes that wrap a pointer, or scoped pointers. |
| It is not destroyed in any form when it goes out of its scope | It destroys itself when it goes out of its scope |
| Pointers are not so efficient as they don't support any other feature. | Smart pointers are more efficient as they have an additional feature of memory management. |
| They are very labor-centric/manual. | They are automatic/pre-programmed in nature. |