Memory and RAII

Memory and RAII

Object lifetime, stack vs heap, smart pointers, and resource safety patterns.

Memory and RAII

Lifetime categories

Prefer RAII

Resource Acquisition Is Initialization means the object owns the resource and releases it in the destructor.

class FileHandle {
public:
    explicit FileHandle(std::FILE* file) : file_{file} {}
    ~FileHandle() {
        if (file_) std::fclose(file_);
    }

private:
    std::FILE* file_{};
};

Smart pointers

auto p = std::make_unique<int>(42);
auto s = std::make_shared<std::string>("hello");
std::weak_ptr<std::string> weak = s;

Move semantics

std::vector<std::string> names;
std::string temp = "Ada";
names.push_back(std::move(temp));

Best-practice reminders

Custom deleters

using FilePtr = std::unique_ptr<std::FILE, int(*)(std::FILE*)>;
FilePtr file(std::fopen("data.txt", "r"), &std::fclose);

Custom deleters let smart pointers manage non-memory resources such as file handles, sockets, or OS descriptors.

Views are not owners

Exception-safety checklist