shared_ptr header file
The shared_ptr
header file in C++ is used to include the necessary definitions and declarations for the shared_ptr
class template. The shared_ptr
class template is part of the C++ Standard Library and is used for shared ownership of dynamically allocated objects.
Here are the steps to include and use the shared_ptr
header file in C++:
- Include the
<memory>
header: To use theshared_ptr
class template, you need to include the<memory>
header file in your C++ program. This header file contains the necessary definitions for theshared_ptr
class template.
#include <memory>
- Create a
shared_ptr
object: Once the<memory>
header file is included, you can create ashared_ptr
object to manage the ownership of a dynamically allocated object. Theshared_ptr
class template takes the type of the object it manages as a template argument.
std::shared_ptr<int> ptr(new int);
In the example above, a shared_ptr
object named ptr
is created to manage the ownership of an int
object. The new
keyword is used to dynamically allocate the int
object, and the resulting pointer is passed to the shared_ptr
constructor.
- Access the managed object: You can access the managed object using the
->
or*
operators, just like you would with a raw pointer.
*ptr = 42; // Assign a value to the managed int object
std::cout << *ptr << std::endl; // Print the value of the managed int object
In the example above, the value 42
is assigned to the managed int
object using the *ptr
dereference operator. The value is then printed using std::cout
.
- Automatic deallocation: The
shared_ptr
class template automatically deallocates the managed object when it is no longer needed. This is done using a technique called reference counting. Theshared_ptr
keeps track of the number ofshared_ptr
instances that point to the same managed object. When the lastshared_ptr
instance is destroyed or reset, the managed object is deallocated.
ptr.reset(); // Release ownership of the managed object
In the example above, the reset()
member function is used to release ownership of the managed object. If there are no other shared_ptr
instances pointing to the same managed object, the object is deallocated.
- Shared ownership: One of the key features of
shared_ptr
is that it allows multipleshared_ptr
instances to share ownership of the same managed object. This means that you can assign ashared_ptr
to anothershared_ptr
, and they both will manage the same object.
std::shared_ptr<int> ptr1(new int);
std::shared_ptr<int> ptr2 = ptr1; // Shared ownership
In the example above, ptr1
and ptr2
both manage the same int
object. The reference count is incremented when ptr2
is assigned the value of ptr1
, and it is decremented when either ptr1
or ptr2
is destroyed or reset.
- Avoiding memory leaks: Using
shared_ptr
helps prevent memory leaks because the managed object is automatically deallocated when it is no longer needed. This ensures that the memory allocated withnew
is properly freed.
std::shared_ptr<int> ptr(new int);
// ...
// No need to manually deallocate the object
In the example above, the managed int
object is automatically deallocated when the shared_ptr
instance ptr
goes out of scope.
That covers the steps for including and using the shared_ptr
header file in C++.