c-plus-plus
  1. c-plus-plus-smart-pointer

C++ Smart Pointers

Smart pointers in C++ are objects that behave like pointers but also have additional functionality such as automatic memory management. They are used to manage dynamic memory allocation and prevent memory leaks.

Syntax

The two most commonly used smart pointers in C++ are std::unique_ptr and std::shared_ptr.

std::unique_ptr<type> pointer_name (new type);
std::shared_ptr<type> pointer_name (new type);

The std::unique_ptr can only have one owner and is responsible for deleting the object it points to when it goes out of scope. The std::shared_ptr can have multiple owners and uses a reference count to manage ownership and automatically delete the object when the last owner goes out of scope.

Example

#include <iostream>
#include <memory>

class Test {
public:
    Test() {std::cout << "Test constructor\n";}
    ~Test() {std::cout << "Test destructor\n";}
    void hello() {std::cout << "Hello World!\n";}
};

int main() {
    std::unique_ptr<Test> unique_test(new Test);
    std::shared_ptr<Test> shared_test(new Test);
    unique_test->hello();
    shared_test->hello();
    return 0;
}

Output

Test constructor
Test constructor
Hello World!
Hello World!
Test destructor

Explanation

In the above example, we have created two instances of Test class using std::unique_ptr and std::shared_ptr. We have called the hello method on both instances, which prints "Hello World!" to the console. Once the main function goes out of scope, the unique_test object is deleted automatically by the std::unique_ptr destructor, while the shared_test object stays alive because it has another owner.

Use

Smart pointers are used to manage dynamic memory allocation and prevent memory leaks. They remove the need for manual memory allocation and deallocation, making code safer and easier to maintain.

#include <iostream>
#include <memory>

int main() {
    std::unique_ptr<int[]> array(new int[10]);
    for (int i = 0; i < 10; i++) {
        array[i] = i;
    }
    for (int i = 0; i < 10; i++) {
        std::cout << array[i] << " ";
    }
    std::cout << std::endl;
    return 0;
}

In the above example, we have created a dynamic array of integers using std::unique_ptr. We have initialized the array with values 0 to 9 and printed them to the console. Once the main function goes out of scope, the memory is automatically deallocated by the std::unique_ptr destructor.

Important Points

  • Smart pointers are used to manage dynamic memory allocation and prevent memory leaks.
  • The two most commonly used smart pointers in C++ are std::unique_ptr and std::shared_ptr.
  • std::unique_ptr can only have one owner and is responsible for deleting the object it points to when it goes out of scope.
  • std::shared_ptr can have multiple owners and uses a reference count to manage ownership and automatically delete the object when the last owner goes out of scope.

Summary

In summary, smart pointers in C++ are essential for managing dynamic memory allocation and preventing memory leaks. They remove the need for manual memory allocation and deallocation, making code safer and easier to maintain. The two most commonly used smart pointers in C++ are std::unique_ptr and std::shared_ptr, each with its own unique properties and use cases.

Published on: