C++ Core
Object Lifetime
Automatic, dynamic, or static — how long an object lives, and RAII.
Concept core idea Watch out easy to get wrong
Concept
Key concepts
- Three storage durations: automatic (stack — lives to the end of its
{}scope, dtor runs automatically), dynamic (heap — fromnewuntildelete, else leak), static/global (whole program). - 'Lifetime = scope' applies only to automatic objects; within a scope they're destroyed in reverse order of creation.
- Returning
&x/ a reference to a local = dangling (its stack frame is gone). Fixes: return by value, heap + transfer ownership, or a caller-supplied buffer. - RAII: acquire a resource in the constructor, release it in the destructor -> cleanup is automatic and exception-safe (destructors run during stack unwinding). Basis of smart pointers,
lock_guard,fstream. - C++ gives deterministic cleanup — objects die at predictable scope-exit points (unlike garbage-collected languages).
- A temporary lives to the end of the full expression (
;); a temporary bound to aconstreference has its lifetime extended to the reference.
Dangling vs valid, RAII, and a static local
int* bad() { int x = 5; return &x; } // dangling: x dies at }
int good() { int x = 5; return x; } // fine: value copied out
void demo() {
std::lock_guard<std::mutex> lock(m); // RAII: lock acquired here
// ... even if the code below throws ...
} // dtor runs at scope exit -> lock released
int next() { static int n = 0; return ++n; } // 1, 2, 3... kept across calls Watch out
Easily confused
automatic vs dynamic vs static lifetime
Automatic (stack): to the end of scope. Dynamic (heap): until you
delete. Static/global: the whole program.dangling vs valid return
Return
&local = dangling (frame gone). Return by value, or heap + ownership, or a caller-supplied buffer = valid.RAII vs manual cleanup
RAII: the destructor releases automatically at scope exit (even on exceptions). Manual: you must remember to free/close/unlock — easy to forget.
Test yourself
7 questions on this lesson. Write your answers from memory, then compare them with the model answers side by side.