Storage duration specifiers
From cppreference.com
-
auto
- automatic storage duration. (deprecated) -
register
- automatic storage duration. Also hints to the compiler to place the variable in the processor's register. (deprecated) -
static
- static storage duration and internal linkage -
extern
- static storage duration and external linkage -
thread_local
- thread storage duration. (since C++11)
-
Contents |
[edit] Explanation
[edit] Storage duration
All variables in a program have one of the following storage durations:
-
automatic storage duration. The variable is allocated at the beginning of the enclosing code block and deallocated on end. All non-global variables have this storage duration, except those declared
static
,extern
orthread_local
.
-
automatic storage duration. The variable is allocated at the beginning of the enclosing code block and deallocated on end. All non-global variables have this storage duration, except those declared
-
static storage duration. The variable is allocated when the program begins and deallocated when the program ends. Only one instance of the variable exists. All global variables have this storage duration, plus those declared with
static
orextern
.
-
static storage duration. The variable is allocated when the program begins and deallocated when the program ends. Only one instance of the variable exists. All global variables have this storage duration, plus those declared with
-
thread storage duration (since C++11). The variable is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the variable. Only variables declared
thread_local
have this storage duration.thread_local
can only be declared for global variables, plus those declared withstatic
orextern
.
-
thread storage duration (since C++11). The variable is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the variable. Only variables declared
- dynamic storage duration. The variable is allocated and deallocated per request by using dynamic memory allocation functions.
[edit] Linkage
Linkage refers to the ability of a variable or function to be referred to in other scopes. If a variable or function with the same identifier is declared in several scopes, but cannot be referred to from all of them, then several instances of the variable are generated. The following linkages are recognized:
- no linkage. The variable can be referred to only from the scope it is in. All variables with automatic, thread and dynamic storage durations have this linkage.
-
internal linkage. The variable can be referred to from all scopes in the current translation unit. All variables with static storage duration which are either declared
static
, orconst
but notextern
, have this linkage.
-
internal linkage. The variable can be referred to from all scopes in the current translation unit. All variables with static storage duration which are either declared
-
external linkage. The variable can be referred to from the scopes in the other translation units. All variables with static storage duration have this linkage, except those declared
static
, orconst
but notextern
.
-
external linkage. The variable can be referred to from the scopes in the other translation units. All variables with static storage duration have this linkage, except those declared
[edit] Keywords
auto, register, static, extern, thread_local
[edit] Example
This section is incomplete |