Memory Management & RAII
Resource Acquisition Is Initialization — tie memory and resources to object lifetime for safe, leak-free code.
In C++, you have direct control over memory. While this gives you power, it also gives you the responsibility to free every byte you allocate. RAII is the “secret sauce” that makes modern C++ safe and exception-proof.
Two regions hold your data. The stack grows and shrinks automatically as
functions are called and return; the heap is the pool you manage by hand with
new and delete. Step through the program below to see frames push and pop, a
block get allocated and freed, and the two classic bugs — a dangling pointer and
a leak — appear.
The Problem: Manual Management
In old-style C++, you would allocate memory with new and free it with delete.
If you forgot the delete, or if an exception was thrown before you reached it,
you had a memory leak.
The Solution: RAII
RAII (Resource Acquisition Is Initialization) is a pattern where a resource (memory, a file, a database lock) is tied to the lifetime of an object.
- Constructor: Acquires the resource.
- Destructor: Releases the resource.
Because the C++ compiler guarantees that a local object’s destructor is called when it goes out of scope, the resource is always freed, even if an error occurs.
// Modern C++ (Smart Pointers)
#include <memory>
void example() {
// std::unique_ptr uses RAII to automatically delete
// the object when 'ptr' goes out of scope.
auto ptr = std::make_unique<MyClass>();
// No "delete" required!
} // ptr is destroyed here, calling 'delete'Smart Pointers
Modern C++ provides smart pointers in the <memory> header that implement RAII:
std::unique_ptr: Owns a resource exclusively. It cannot be copied, only moved.std::shared_ptr: Uses reference counting. The resource is deleted only when the last pointer to it is destroyed.
Takeaways
- RAII ties resource lifetime to object scope.
- It prevents memory leaks and makes code exception-safe.
- Always prefer smart pointers (
unique_ptr,shared_ptr) over rawnewanddelete.