Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    C++C++

    C++ fundamentals

    • Your first C++ program
    • Variables and types
    • Operators and input
    • Conditions: if and switch
    • Loops

    Functions and collections

    • Functions
    • References: by value or by reference?
    • Strings and arrays
    • std::vector
    • map, set and unordered_map

    Memory and pointers

    • Pointers
    • Stack, heap, new and delete
    • Smart pointers and RAII

    Classes and OOP

    • struct and class
    • Constructors and destructors
    • Inheritance and virtual functions
    • Templates

    The STL and professional C++

    • Algorithms and lambdas
    • Exceptions
    • Recursion
    • Project: a grade book
    C++

    Memory and pointers · Lesson 13 of 21

    Smart pointers and RAII

    unique_ptr and shared_ptr, and the idea that makes C++ safe.

    11 minutes2 games

    RAII (Resource Acquisition Is Initialization) is C++'s most important idea: tie every resource (memory, a file, a connection, a lock) to a stack object. The object acquires it in its constructor and releases it in its destructor. Since the destructor runs automatically when the object leaves scope, the resource is never forgotten, even when an exception is thrown.

    C++
    #include <iostream>
    #include <memory>
    #include <string>
    
    struct Course {
        std::string title;
        explicit Course(std::string t) : title(std::move(t)) { std::cout << "open " << title << "\n"; }
        ~Course() { std::cout << "close " << title << "\n"; }
    };
    
    int main() {
        {
            std::unique_ptr<Course> c = std::make_unique<Course>("C++");
            std::cout << "studying " << c->title << "\n";
        }   // c leaves scope here: the Course is deleted automatically
        std::cout << "after the block\n";
        return 0;
    }

    unique_ptr means one owner: it can't be copied, but ownership can be moved with std::move. shared_ptr means many owners: it counts holders and frees the object when the last one lets go.

    C++
    #include <iostream>
    #include <memory>
    #include <vector>
    
    int main() {
        auto a = std::make_unique<int>(5);
        auto b = std::move(a);              // ownership moves to b
        std::cout << (a == nullptr) << " " << *b << "\n";
    
        auto shared = std::make_shared<int>(7);
        std::vector<std::shared_ptr<int>> owners = {shared, shared};
        std::cout << "owners: " << shared.use_count() << "\n";
        owners.clear();
        std::cout << "owners: " << shared.use_count() << "\n";
        return 0;
    }

    make_shared: count 1

    two copies in the vector: count 3

    clear(): count 1

    shared leaves scope: count 0, so it's freed

    Important

    Start with unique_ptr; use shared_ptr only when ownership is genuinely shared, since its counter has a cost. Beware: two shared_ptrs pointing at each other (a cycle) never reach zero and leak. The fix is making one of them a std::weak_ptr.

    What's the output?

    #include <iostream>
    #include <memory>
    struct T {
        int id;
        T(int i) : id(i) {}
        ~T() { std::cout << "bye" << id << " "; }
    };
    int main() {
        auto a = std::make_unique<T>(1);
        {
            auto b = std::make_unique<T>(2);
        }
        std::cout << "end ";
    }

    What will this code print?

    Interview question

    What is RAII? How do unique_ptr, shared_ptr and weak_ptr differ?

    Exercise

    Create a std::unique_ptr<std::vector<int>> holding {3, 1, 4} with std::make_unique, add 1 and 5 with push_back through ->, and print its size.

    Expected output: 5

    C++
    #include <iostream>
    #include <memory>
    #include <vector>
    
    int main() {
        
        return 0;
    }

    Learn more from

    • cppreference: RAIIen.cppreference.com
    • cppreference: std::unique_ptren.cppreference.com
    • learncpp: std::unique_ptrlearncpp.com
    • C++ Core Guidelines: Resource managementisocpp.github.io

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousStack, heap, new and deleteNextstruct and class
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.