Shared pointer
Shared pointers are a type of smart pointer in C++ that manage the lifetime of dynamically allocated objects. Multiple shared_ptr instances can share ownership of an object, and the object is automatically destroyed when the last shared_ptr owning it is destroyed.
Syntax:std::shared_ptr<ClassName> ptr = std::make_shared<ClassName>(constructor_args);
Code Example
Explanation
Including Required Headers
memory.h is needed for smart pointer.
#include
Class definition
class MyClass {
public:
string name;
MyClass(string name) : name(name) {
std::cout << name << " constructor" << endl;
}
~MyClass() {
std::cout << name << " destructor" << endl;
}
};
MyClass has a constructor that takes a string argument and prints a message when an object is created. The destructor prints a message when an object is destroyed.
main function
MyClass* ptr = new MyClass("raw pointer");
A raw pointer ptr is created, pointing to a MyClass object. This is the traditional way of managing dynamically allocated objects.
shared_ptr<MyClass> ptr_s = make_shared<MyClass>("smart pointer");
A shared_ptr ptr_s is created using make_shared. The destructor is automatically called when ptr_s goes out of scope, releasing the memory.
shared_ptr<MyClass> ptr_1 = make_shared<MyClass>("smart pointer1");
cout << "There is " << ptr_1.use_count() << " pointer to the object" << endl;
ptr_1 is another shared_ptr created with make_shared. The use_count() method returns the number of shared_ptr instances managing the current object (initially 1).
shared_ptr<MyClass> ptr_2 = ptr_1; // associate one more pointer to the same object
cout << "There are " << ptr_1.use_count() << " pointers to the object" << endl;
ptr_2 is another shared_ptr that shares ownership of the same object as ptr_1. use_count() returns 2.
ptr_2.reset(); // or ptr_2 == nullptr; reset ptr_2, so it's not pointing to any object
cout << "There is " << ptr_1.use_count() << " pointer to the object" << endl;
ptr_2 is reset, reducing the reference count to 1. use_count() returns 1.
ptr_1 = nullptr; // or ptr_1.reset(), reset ptr_1, so no pointer to the object, the object is deleted automatically (destructor called)
cout << "There is " << ptr_1.use_count() << " pointer to the object" << endl;
ptr_1 is reset, reducing the reference count to 0, and the object is automatically deleted. use_count() returns 0.
delete ptr;
The raw pointer ptr is explicitly deleted to prevent memory leaks.
Output
The output of the above program will be:
raw pointer constructor
smart pointer constructor
smart pointer desctructor
smart pointer1 constructor
There is 1 pointer to the object
There are 2 pointers to the object
There is 1 pointer to the object
smart pointer1 desctructor
There is 0 pointer to the object
raw pointer desctructor
Conclusion
Using shared_ptr in C++ helps manage dynamically allocated objects automatically, reducing the risk of memory leaks and making the code easier to manage. It keeps track of the reference count and destroys the managed object when the count reaches zero. This is especially useful in scenarios involving multiple owners of the same resource.