C++ Reference ยท Memory and Utilities
RAII in C++ (resource acquisition is initialization)
RAII ties a resource's lifetime to an object: acquire in the constructor, release in the destructor. Learn it with lock guards, scope guards and exceptions.
RAII, short for resource acquisition is initialization, is the central C++ idiom for managing anything that must be released: memory, files, locks, sockets, transactions. A resource is owned by an object. The constructor acquires it, the destructor releases it. Because destructors run automatically when a scope ends, whether by reaching the closing brace, return, break or an exception, cleanup cannot be forgotten.
It is a concept rather than a library feature, but the standard library is full of it: std::vector, std::string, std::unique_ptr, std::fstream, std::lock_guard, std::jthread.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
How it works
- Acquire in the constructor. If acquisition fails, throw; a constructor that throws leaves no half-built object, and members already built are destroyed.
- Release in the destructor. It runs for every normal exit and during stack unwinding. It should not throw: destructors are
noexceptby default. - Control copying. Decide what copying means. Usually the owner is move-only (delete the copy operations, provide a move), or copying makes a deep copy, or ownership is shared with a count (
shared_ptr). This is the rule of five. - Scope is lifetime. Locals are destroyed in reverse order of declaration; members in reverse order of their declaration in the class.
Usage notes
- Do not write
newanddeletepairs in application code. If a function needsdeletein a cleanup branch, wrap the resource instead. - The standard classes you should reach for first:
std::unique_ptr(single owner),std::shared_ptr,std::vectorandstd::string(memory),std::lock_guard/std::scoped_lock/std::unique_lock(mutexes),std::ifstream/std::ofstream(files),std::jthread(joins in its destructor). - A "scope guard" runs arbitrary code on exit. C++ has no standard one yet (
std::experimental::scope_exitis a TS), but a small class that stores a callable is easy to write. - Name the guard!
std::lock_guard<std::mutex>(m);creates and immediately destroys a temporary, which locks nothing for the rest of the scope. - Exceptions: because cleanup is in destructors, you rarely need
try/finally. C++ has nofinallykeyword for this reason. - A resource that is not released by the end of
mainbecausestd::exitorstd::abortwas called will skip local destructors. Return frommainor throw instead. - A destructor that throws during unwinding calls
std::terminate.