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 or extern
    • Only one instance exists program-wide. Allocated at program start, deallocated at the end.
  • Thread-local:
    • Only objects marked as thread_local
    • Allocated at thread-start, deallocated a thread end
  • Dynamic:
    • For objects allocated via dynamic memory management (new, delete)
    • Lifetime not bound to any enclosing scope