Overview of C++ Storage Durations
- Automatic:
- Normal, block level-scoped variables
- Allocated at the start of the enclosing code block, deallocated at the end
- Static:
- Global variables and variables declared
static
orextern
- Only one instance exists program-wide. Allocated at program start, deallocated at the end.
- Global variables and variables declared
- Thread-local:
- Only objects marked as
thread_local
- Allocated at thread-start, deallocated a thread end
- Only objects marked as
- Dynamic:
- For objects allocated via dynamic memory management (
new
,delete
) - Lifetime not bound to any enclosing scope
- For objects allocated via dynamic memory management (