Storage Durations in C++

C++
Author

Quasar

Published

May 22, 2025

Introduction

Two key properties of an object in C++ are storage and linkage.

Note

Storage Duration. The storage duration is the property of an object that defines the minimum potential lifetime of the storage containing the object. The storage duration is determined by the construct used to create the object.

An object in C++ has one of the following storage durations:

  • automatic : Automatic means that the storage is allocated at the start of the scope. Most local variables have automatic storage duration (except those declared as static, extern or thread_local ).

  • static : The storage for an object is allocated when the program begins usually before the main() function starts and is deallocated when the program ends. There’s only one instance of such an object in the whole program.

  • thread : The storage for an object is tied to a thread: it’s started when a thread begins and is deallocated when the thread ends. Each thread has its own copy of the object.

  • dynamic : The storage for an object is allocated and deallocated using explicit dynamic memory allocation functions.

The definition of the second property linkage from the standard is as follows:

Note

Linkage. A name is said to have a linkage when it can denote the same object, reference , function type, template, namespace as a name introduced in another scope.

We can have several linkage types:

  • external linkage : External means that the name can be referred to in scopes within the same translation unit or outside. Non-const global variables have extern storage duration.