Scope and Lifetime of Variables in C++
Scope determines where a variable's name can be found by name lookup; object lifetime determines when the object exists.
Summary
Scope determines where a variable's name can be found by name lookup; object lifetime determines when the object exists. Storage duration describes how long its storage is available. These properties are related but not interchangeable.
A block-scope name is visible from its declaration to the end of its enclosing block, except where another declaration hides it. Shadowing hides a name without destroying the outer object.
| Storage duration | Behavior | Example |
|---|---|---|
| Automatic | An ordinary local object's lifetime ends when execution leaves its block | Non-static local variable |
| Static | Storage persists for the program; an initialized local static object survives function calls | Local static or non-thread-local namespace-scope object |
| Thread | A distinct object is associated with each thread | thread_local variable |
| Dynamic | An allocated object is not destroyed merely because the allocating function returns | Object created by new |
Returning a pointer or reference to an ordinary local object leaves a dangling access path after return. Losing the last owning pointer to an unreleased dynamically allocated object can cause a memory leak. RAII connects resource ownership to object lifetime so destructors perform cleanup, including during exception unwinding. Shared local static state may require synchronization.
Common Misconceptions: Leaving a name's scope does not necessarily end the object's lifetime. Shadowing does not destroy the hidden object. Namespace-scope objects can have static storage duration with either internal or external linkage.
🧠 Key Concepts
- Name scope
- Object lifetime
- Storage duration
- Shadowing
- Local static objects
- Thread storage duration
- Dynamic allocation
- Dangling pointers
- Memory leaks
- RAII
🧠 Quick Check
See what you remember from the summary.
Which best describes shadowing in a nested C++ block?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Scope and Lifetime of Variables in C++
📘 Overview Scope determines where a variable's name can be used; lifetime determines when its object exists. Storage duration governs how long storage is available, so leaving a name's scope does not always end the object's lifetime.
🧠 Key Idea Visibility and existence are separate properties: a local static object survives between function calls, while a dynamically allocated object can outlive the pointer variable that originally referenced it.
⚔️ Core Details: - A block-scope variable's name is visible from its declaration to the end of its enclosing block, except where hidden by another declaration. - An ordinary non-static local object's lifetime ends when execution leaves its block, including through return or exception unwinding. - A block-scope static object persists between function calls after initialization. - Non-thread-local namespace-scope objects have static storage duration, regardless of whether their names have internal or external linkage. - An object created by new has dynamic storage duration and is not destroyed merely because the allocating function returns. - A thread_local variable has thread storage duration, with a distinct object associated with each thread.
🎯 Why It Matters: - Returning a pointer or reference to an ordinary local object creates a dangling access path after the function returns. - Losing the last owning pointer to a dynamically allocated object without releasing it can cause a memory leak. - RAII ties resource ownership to object lifetime, enabling automatic cleanup through destructors. - Local static state persists across calls and may require synchronization when shared by multiple threads.
🧠 Quick Recall: - Scope - The region of a program in which a declaration's name can be found by name lookup. - Object lifetime - The execution interval beginning after suitable storage and initialization and ending when destruction starts or storage is released or reused. - Storage duration - The storage category: automatic, static, thread, or dynamic. - Shadowing - An inner declaration hides an outer declaration with the same name without destroying the outer object. - Dangling pointer or reference - A pointer or reference to an object whose lifetime has ended.
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Science notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.