liveBook · Manning

Resource acquisition is initialization

As explained in Learn C++ by Example

Resource acquisition is initialization is a C++ resource-management technique that acquires a resource during object construction and releases it during object destruction.

Resource acquisition is initialization (RAII) ties a resource’s lifetime to an object: acquire it during construction and release it during destruction. Scope then handles cleanup automatically, even when the object is stored inside a container. section 6.2.2

RAII appears in several common ownership patterns:

  • A vector destroys its contained objects when it leaves scope, but raw pointers in the vector only destroy the pointer values, not their targets. section 6.2.2
  • A unique_ptr owns one underlying pointer; copying is disabled, moving is allowed, and deletion occurs by default at destruction. section 6.2.2
  • A shared_ptr permits multiple owners and destroys the object after the final owner is destroyed or reset. section 6.2.2
  • A custom deleter lets RAII perform cleanup other than delete, such as destroying a coroutine handle. section 8.3.4

Smart pointers in a vector

void very_smart() { std::vector<SmartPointer<Blob>> blobs; blobs.emplace_back(make_smart_pointer<StepperBlob>()); } When blobs leaves scope, its element is destroyed, and the smart pointer performs its configured cleanup. section 6.2.2

How Learn C++ by Example covers it

How deep it goes
Explains construction, destruction, ownership, custom deleters, and rule-of-zero consequences without detailing compiler-generated lifetime mechanics.
Examples
Uses vectors of smart pointers and coroutine promise pointers as concrete lifetime-management examples.
What it assumes
Assumes familiarity with C++ objects, constructors, destructors, pointers, scope, and templates.
What you can do after
Apply it via C++ containers, smart pointers, and custom deleters to automate resource cleanup.
How it connects
Connects object lifetime with smart pointers, containers, ownership, custom deleters, coroutines, and the rule of zero.

Also explained in

Compare how 4 books explain resource acquisition is initialization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.