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
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- Function Interposition in C with an example of user defined malloc()
- User Defined Literals in C++
- Multi-set for user defined data type
- User defined Data Types in C++
- 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++?
- 2D Vector In C++ With User Defined Size
- Chat application between two processes using signals and shared memory
- What are Wild Pointers? How can we avoid?
- Declare a C/C++ function returning pointer to array of integer pointers
- Introduction of Smart Pointers in C++ and It's Types
- Dangling, Void , Null and Wild Pointers
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- Pointers in C/C++ with Examples
- Pointers vs References in C++
- Computing index using pointers returned by STL functions in C++
- Program to reverse an array using pointers
- Applications of Pointers in C/C++
- C++ Program to compare two string using pointers
- Program to Reverse a String 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 email@example.com. 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.