Klasa pamięci C ++: lokalna, globalna, statyczna, rejestrowa i lokalna wątku

W tym artykule dowiesz się o różnych klasach pamięci w C ++. Mianowicie: lokalna, globalna, statyczna lokalna, rejestrowa i lokalna wątku.

Każda zmienna w C ++ ma dwie cechy: typ i klasę pamięci.

Typ określa typ danych, które mogą być przechowywane w zmiennej. Na przykład: int, float, charitd.

A klasa pamięci kontroluje dwie różne właściwości zmiennej: okres istnienia (określa, jak długo zmienna może istnieć) i zasięg (określa, która część programu ma do niej dostęp).

W zależności od klasy przechowywania zmiennej można ją podzielić na 4 główne typy:

  • Zmienna lokalna
  • Zmienna globalna
  • Statyczna zmienna lokalna
  • Zarejestruj zmienną
  • Pamięć lokalna wątku

Zmienna lokalna

Zmienna zdefiniowana wewnątrz funkcji (zdefiniowana w treści funkcji między nawiasami klamrowymi) nazywana jest zmienną lokalną lub zmienną automatyczną.

Jego zakres jest ograniczony tylko do funkcji, w której została zdefiniowana. Mówiąc prościej, zmienna lokalna istnieje i można uzyskać do niej dostęp tylko wewnątrz funkcji.

Życie zmiennej lokalnej kończy się (jest niszczona), gdy funkcja kończy działanie.

Przykład 1: zmienna lokalna

 #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; )

Zmienna var nie może być używana wewnątrz, test()a var1 nie może być używana wewnątrz main()funkcji.

Słowo kluczowe autobyło również używane do definiowania zmiennych lokalnych wcześniej jako:auto int var;

Ale po C ++ 11 automa inne znaczenie i nie powinien być używany do definiowania zmiennych lokalnych.

Zmienna globalna

Jeśli zmienna jest zdefiniowana poza wszystkimi funkcjami, nazywana jest zmienną globalną.

Zakres zmiennej globalnej obejmuje cały program. Oznacza to, że można go używać i zmieniać w dowolnej części programu po jego zadeklarowaniu.

Podobnie, jego życie kończy się dopiero po zakończeniu programu.

Przykład 2: zmienna globalna

 #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.

Interesujące artykuły...