Last Updated : 15 May, 2025
In C++, pointers are the variables that stores the memory addresses. They are extensively used in dynamic memory location to store the address of allocated memory. But they bring a lot of issues.
Problems with Normal PointersExample:
C++
#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.
Smart PointersSmart pointer is wrapper class over a pointer that acts as a pointer but automatically manages the memory it points to. It ensures that memory is properly deallocated when no longer needed, preventing memory leaks. It is a part of <memory> header file.
Smart pointers are implemented as templates so we can create a smart pointer to any type of memory.
Types of Smart PointersC++ libraries provide implementations of smart pointers in the following types:
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.
Syntax:
C++
where,
Example:
C++
#include <bits/stdc++.h>
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;
}
2. unique_ptrNote: auto_ptr is deprecated after C++11 and it remove after C++ version 17.
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.
Example:
C++
#include <bits/stdc++.h>
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;
}
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.
Example:
C++
#include <bits/stdc++.h>
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;
}
4. 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.
Example:
C++
#include <bits/stdc++.h>
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;
}
If you're curious about what the differences are between all of them., refer this article - auto_ptr vs unique_ptr vs shared_ptr vs weak_ptr
Pointers vs Smart PointersDifferences between pointers and smart pointers are mentioned below:
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.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4