Shared Pointers :
A std::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 std::shared_ptr. So, the counter is incremented each time a new pointer points to the resource and decremented when 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 reference count is greater than zero i.e. until all copies of std::shared_ptr have been deleted.
When to use: We should use shared_ptr when we want to assign one raw pointer to multiple owners.
For more information and details about shared and other smart pointers, please read here.
User Defined Implementation of Shared pointers:
--- Shared pointers ptr1 --- Address pointed : 0x1cbde70 Counter Value : 1 --- Shared pointers ptr1, ptr2 --- Address pointed : 0x1cbde70 Counter Value : 2 Address pointed : 0x1cbde70 Counter Value : 2 --- Shared pointers ptr1, ptr2, ptr3 --- Address pointed : 0x1cbde70 Counter Value : 3 Address pointed : 0x1cbde70 Counter Value : 3 Address pointed : 0x1cbde70 Counter Value : 3 --- Shared pointers ptr1, ptr2 --- Address pointed : 0x1cbde70 Counter Value : 2 Address pointed : 0x1cbde70 Counter Value : 2 --- Shared pointers ptr1 --- Address pointed : 0x1cbde70 Counter Value : 1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- User Defined Literals in C++
- 2D vector in C++ with user defined size
- User defined Data Types in C++
- Function Interposition in C with an example of user defined malloc()
- Multi-set for user defined data type
- How to create an unordered_map of user defined class in C++?
- How to create an unordered_set of user defined class or struct in C++?
- Chat application between two processes using signals and shared memory
- Applications of Pointers in C/C++
- Pointers and References in C++
- Features and Use of Pointers in C/C++
- Pointers in C/C++ with Examples
- Pointers vs References in C++
- How many levels of pointers can we have in C/C++
- What are Wild Pointers? How can we avoid?
- Why do we need reference variables if we have pointers
- Program to Reverse a String using Pointers
- Difference between Iterators and Pointers in C/C++ with Examples
- C++ Program to compare two string using pointers
- Program to reverse an array using pointers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : prakashgibbi