std::unique_ptr


Concept: std::unique_ptr

std::unique_ptr is a smart pointer in C++ that provides exclusive ownership of a dynamically allocated object. When a std::unique_ptr goes out of scope, it automatically deletes the object it manages, preventing memory leaks. The key characteristics of std::unique_ptr are that it cannot be copied, but it can be moved, allowing ownership to be transferred.

Key Features of std::unique_ptr

Example 1: basic usage

#include <iostream>
#include <memory>

class MyClass {
public:
    MyClass() { std::cout << "MyClass Constructor" << std::endl; }
    ~MyClass() { std::cout << "MyClass Destructor" << std::endl; }
    void sayHello() const { std::cout << "Hello from MyClass" << std::endl; }
};

int main() {
    std::unique_ptr<MyClass> ptr = std::make_unique<MyClass>();
    ptr->sayHello();  // Access the object using the unique_ptr

    // No need to manually delete the object, it will be destroyed automatically
    return 0;
}

Explanation:

Moving std::unique_ptr

Since std::unique_ptr cannot be copied (to prevent multiple pointers from owning the same resource), ownership can be transferred using move semantics. This is achieved with std::move.

Example 2: Transferring Ownership

#include <iostream>
#include <memory>

class MyClass {
public:
    MyClass() { std::cout << "MyClass Constructor" << std::endl; }
    ~MyClass() { std::cout << "MyClass Destructor" << std::endl; }
    void sayHello() const { std::cout << "Hello from MyClass" << std::endl; }
};

int main() {
    std::unique_ptr<MyClass> ptr1 = std::make_unique<MyClass>();

    // Transfer ownership from ptr1 to ptr2
    std::unique_ptr<MyClass> ptr2 = std::move(ptr1);

    if (!ptr1) {
        std::cout << "ptr1 is now null" << std::endl;
    }

    ptr2->sayHello();  // Access the object through ptr2

    return 0;
}

Explanation:

Example 3: Custom Deleters

std::unique_ptr allows you to specify a custom deleter, which is a function or function object that is called when the unique_ptr goes out of scope. This is useful when managing resources that require special cleanup, such as files or network sockets.

#include <iostream>
#include <memory>
#include <cstdio>  // For FILE*

struct FileDeleter {
    void operator()(FILE* fp) const {
        if (fp) {
            std::cout << "Closing file" << std::endl;
            std::fclose(fp);
        }
    }
};

int main() {
    std::unique_ptr<FILE, FileDeleter> filePtr(std::fopen("example.txt", "w"));

    if (filePtr) {
        std::fprintf(filePtr.get(), "Hello, file!\n");
    }

    // filePtr goes out of scope, and FileDeleter is invoked to close the file

    return 0;
}

Explanation:

Common Use Cases for std::unique_ptr

Summary

std::unique_ptr is a fundamental tool in modern C++ for ensuring safe and efficient resource management, making it easier to avoid memory leaks and dangling pointers.

Previous Page | Course Schedule | Course Content