Shared Pointer In C++
INTRODUCTION
- A shared pointer in C++ is a type of smart pointer that enables several pointers to share control over an object that has been allocated dynamically.
- By automatically reallocating the RAM when it is no more required, it aids in controlling the object's lifespan. Shared pointers are defined in the memory> header and are a component of the C++ Standard Library.
Implementation of Shared Pointers
- Reference counting is used to implement shared pointers. Each shared pointer counts the number of points pointing at the same object. The reference count is started at 1 when a shared pointer is formed.
- The reference count increases when a new shared pointer points to the same object. The reference count decreases whenever a shared pointer is deleted or transferred.
- The object is immediately removed when the reference count drops to zero, indicating that no shared pointers are addressing it anymore.
- A shared pointer in C++ is a type of smart pointer that enables several pointers to share control over an object that has been allocated dynamically. It is defined in the memory> header and is a component of the C++ Standard Library.
- To declare a shared pointer in C++, use the std::shared_ptr template and include the memory header.
EXAMPLE
#include#include int main() { std::shared_ptr mySharedPtr(new int(50)); std::cout << "Value: " << *mySharedPtr << std::endl; std::shared_ptr anotherSharedPtr = mySharedPtr; *mySharedPtr = 12; *anotherSharedPtr = 110; std::cout << "Value: " << *mySharedPtr << std::endl; std::cout << "Value: " << *anotherSharedPtr << std::endl; return 0; }
OUTPUT
![SHARED POINTER IN C++](https://static.tutorialandexample.com/cpp/shared-pointer-in-cpp1.png)
Explanation
- The object that mySharedPtr points to has a 42 initial value.
- Both *mySharedPtr and *anotherSharedPtr's values are updated to 100 due to the modifications because they both refer to the same object.
- As a result, we obtain the updated value of 100 when printing the value of *mySharedPtr & *anotherSharedPtr.
ADVANTAGES
- Shared pointers make memory management easier by automatically allocating space when it is no more required. The reference counting system ensures that objects are removed after the last shared pointer accessing them exits the scope to prevent memory leaks.
- Shared pointers make it simple to share resources by allowing several pointers to own the same randomly allocated object. Because all shared pointers jointly manage the object's lifetime, ensuring that it is valid for as long as any shared pointer to it, this enables effective resource sharing.
- Dangling pointer problems are avoided thanks to shared pointers. They ensure that subsequent efforts to obtain the object using a shared pointer will produce a safe behaviour, such as raising an exception, by automatically nullifying the pointer when the specified object is removed.
- Shared pointers, which are a component of the C++ Standard Library, offer a flexible and simple-to-use memory management interface. They have a wide range of compatibility and are simple to build into existing codebases because they can be used with a variety of container classes, algorithms, & libraries that demand intelligent pointers.
DISADVANTAGES
- Due to the extra memory needed for reference counting, shared pointers are more complex than raw pointers. Every shared pointer keeps track of its references, which adds to the memory and processing demands. This overhead, may impact performance, mainly when many shared pointers or allocations and deallocations exist.
- Shared pointers are inappropriate for handling cyclic dependencies among objects, which might result in memory leaks. When objects use shared pointers to refer to one another, the reference count rarely goes to zero, which causes memory leaks. Weak pointers or memory management strategies might be needed to address this, such as using raw or specialized intelligent pointers.
- Shared pointers are not by nature thread-safe; hence they have a limited application in multithreaded environments. Data races and unpredictable behaviour may result from many threads simultaneously changing shared pointers.
- Increased Memory Usage: The shared pointers reference counts require more memory. The cumulative reference counts can significantly increase memory usage if many shared pointers are produced or if the objects' lifetimes are prolonged.
CONCLUSION
As a whole, shared pointers in C++ offer several benefits, such as automatic memory management, simple resource sharing, a reduction in dangling pointers, & flexibility in usage. By automatically reallocating memory when it is no longer required, they improve memory management and lower the possibility of memory leaks. Multiple pointers can jointly own an object thanks to shared pointers, which facilitates effective resource allocation and lifespan management. Shared pointers also provide an easy-to-use interface and support for various C++ components.