Classe de stockage C ++: Local, Global, Static, Register et Thread Local

Dans cet article, vous découvrirez les différentes classes de stockage en C ++. À savoir: local, global, local statique, registre et thread local.

Chaque variable en C ++ a deux caractéristiques: le type et la classe de stockage.

Type spécifie le type de données qui peuvent être stockées dans une variable. Par exemple: int, float, charetc.

Et, la classe de stockage contrôle deux propriétés différentes d'une variable: la durée de vie (détermine combien de temps une variable peut exister) et la portée (détermine quelle partie du programme peut y accéder).

Selon la classe de stockage d'une variable, elle peut être divisée en 4 types principaux:

  • Variable locale
  • Variable globale
  • Variable locale statique
  • Enregistrer la variable
  • Stockage local de thread

Variable locale

Une variable définie à l'intérieur d'une fonction (définie à l'intérieur du corps de la fonction entre accolades) est appelée une variable locale ou une variable automatique.

Sa portée est uniquement limitée à la fonction où elle est définie. En termes simples, la variable locale existe et n'est accessible qu'à l'intérieur d'une fonction.

La vie d'une variable locale se termine (elle est détruite) lorsque la fonction se termine.

Exemple 1: variable locale

 #include using namespace std; void test(); int main() ( // local variable to main() int var = 5; test(); // illegal: var1 not declared inside main() var1 = 9; ) void test() ( // local variable to test() int var1; var1 = 6; // illegal: var not declared inside test() cout << var; )

La variable var ne peut pas être utilisée à l'intérieur test()et var1 ne peut pas être utilisée à l'intérieur de la main()fonction.

Le mot-clé autoétait également utilisé pour définir les variables locales auparavant comme:auto int var;

Mais, après C ++ 11 autoa une signification différente et ne doit pas être utilisé pour définir des variables locales.

Variable globale

Si une variable est définie en dehors de toutes les fonctions, alors elle est appelée une variable globale.

La portée d'une variable globale est l'ensemble du programme. Cela signifie qu'il peut être utilisé et modifié à n'importe quelle partie du programme après sa déclaration.

De même, sa vie ne prend fin qu'à la fin du programme.

Exemple 2: variable globale

 #include using namespace std; // Global variable declaration int c = 12; void test(); int main() ( ++c; // Outputs 13 cout << c < 

Output

 13 14

In the above program, c is a global variable.

This variable is visible to both functions main() and test() in the above program.

Static Local variable

Keyword static is used for specifying a static variable. For example:

… int main() ( static float a;… ) 

A static local variable exists only inside a function where it is declared (similar to a local variable) but its lifetime starts when the function is called and ends only when the program ends.

The main difference between local variable and static variable is that, the value of static variable persists the end of the program.

Example 3: Static local variable

 #include using namespace std; void test() ( // var is a static variable static int var = 0; ++var; cout << var << endl; ) int main() ( test(); test(); return 0; )

Output

 1 2

In the above program, test() function is invoked 2 times.

During the first call, variable var is declared as static variable and initialized to 0. Then 1 is added to var which is displayed in the screen.

When the function test() returns, variable var still exists because it is a static variable.

During second function call, no new variable var is created. The same var is increased by 1 and then displayed to the screen.

Output of above program if var was not specified as static variable

 1 1

Register Variable (Deprecated in C++11)

Keyword register is used for specifying register variables.

Register variables are similar to automatic variables and exists inside a particular function only. It is supposed to be faster than the local variables.

If a program encounters a register variable, it stores the variable in processor's register rather than memory if available. This makes it faster than the local variables.

However, this keyword was deprecated in C++11 and should not be used.

Thread Local Storage

Thread-local storage is a mechanism by which variables are allocated such that there is one instance of the variable per extant thread.

Keyword thread_local is used for this purpose.

Learn more about thread local storage.

Articles intéressants...