How to create and initialize a `std::shared_ptr` object

title: Creating and Initializing a std::shared_ptr Object date: 2021-10-01 tags: C++, Smart Pointers


In C++, std::shared_ptr is a smart pointer that provides automatic memory management for dynamically allocated objects. It allows multiple shared_ptr objects to share ownership of the same object, keeping track of the number of references and automatically deallocating the memory when no more references exist.

To create and initialize a std::shared_ptr object, follow the steps below:

  1. Include the required headers:
    #include <memory>
    
  2. Define the type of object you want to manage:
    class MyClass {
     // class implementation
    };
    
  3. Create a new std::shared_ptr object using the std::make_shared function. This function dynamically allocates the object and initializes it, returning a std::shared_ptr that manages the allocated memory:
    std::shared_ptr<MyClass> ptr = std::make_shared<MyClass>();
    
  4. You can also initialize the std::shared_ptr with a pointer to an existing object using the constructor:
    MyClass* obj = new MyClass();
    std::shared_ptr<MyClass> ptr(obj);
    

It’s important to note that you should avoid using raw pointers with std::shared_ptr. Always prefer using std::make_shared or the constructor of std::shared_ptr to ensure proper memory management.

By using std::shared_ptr, you don’t need to explicitly deallocate the memory when you’re done with the object. The std::shared_ptr will automatically take care of releasing the memory when the reference count reaches zero.

It is also worth mentioning that std::shared_ptr supports custom deleters, allowing you to define your own cleanup actions for the managed object.

By following these steps, you can create and initialize a std::shared_ptr object in C++. Enjoy the benefits of automatic memory management with shared ownership!

#CPlusPlus #SmartPointers