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 local storage duration. (since C++11)
Contents |
[edit] Explanation
[edit] Storage duration
All variables in a program have one of the following storage duration:
- 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 or thread_local.
- 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 or extern.
- thread local 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.
- dynamic storage duration. The variable is allocated and deallocated per request by using dynamic memory allocation functions.
[edit] Linkage
The linkage refers to the ability of a variable to be referred to in other scopes. If the variable is declared in several scopes, but it can not be referred to from all of them, several instances of the variable are generated. The following linkages are recognized:
- no linkage. The variable can be referred to only from the scope they are in. All variables with automatic, thread local 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, or const but not extern, have this linkage.
- 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, or const but not extern.
[edit] Keywords
auto, register, static, extern, thread_local
[edit] Example
This section is incomplete |