shared_ptr
A shared_ptr is a container for raw pointers. It is a reference counting ownership model i.e. it maintains the reference count of its contained pointer in cooperation with all copies of the shared_ptr. So, the counter is incremented each time a new pointer points to the resource and decremented when the destructor of the object is called.
Reference Counting: It is a technique of storing the number of references, pointers, or handles to a resource such as an object, block of memory, disk space, or other resources. An object referenced by the contained raw pointer will not be destroyed until the reference count is greater than zero i.e. until all copies of shared_ptr have been deleted. So, we should use shared_ptr when we want to assign one raw pointer to multiple owners.
Example:
C++
// C++ program to demonstrate shared_ptr #include <iostream> #include <memory> using namespace std; class A { public : void show() { cout << "A::show()" << endl; } }; int main() { shared_ptr<A> p1( new A); cout << p1.get() << endl; p1->show(); shared_ptr<A> p2(p1); p2->show(); cout << p1.get() << endl; cout << p2.get() << endl; // Returns the number of shared_ptr objects // referring to the same managed object. cout << p1.use_count() << endl; cout << p2.use_count() << endl; // Relinquishes ownership of p1 on the object // and pointer becomes NULL p1.reset(); cout << p1.get() << endl; cout << p2.use_count() << endl; cout << p2.get() << endl; return 0; } |
0x625010 A::show() A::show() 0x625010 0x625010 2 2 0 1 0x625010
auto_ptr vs unique_ptr vs shared_ptr vs weak_ptr in C++
Prerequisite – Smart Pointers
Smart Pointer is a pointer-wrapping stack-allocated object. Smart pointers, in plain terms, are classes that wrap a pointer, or scoped pointers.
C++ libraries provide implementations of smart pointers in the following types:
- auto_ptr
- unique_ptr
- shared_ptr
- weak_ptr
They all are declared in a memory header file( #include<memory>) which is used to manage dynamic memory allocation.