Lớp lưu trữ C ++: Local, Global, Static, Register và Thread Local

Trong bài viết này, bạn sẽ tìm hiểu về các lớp lưu trữ khác nhau trong C ++. Cụ thể: cục bộ, toàn cục, cục bộ tĩnh, đăng ký và luồng cục bộ.

Mọi biến trong C ++ đều có hai tính năng: kiểu và lớp lưu trữ.

Kiểu chỉ định kiểu dữ liệu có thể được lưu trữ trong một biến. Ví dụ: int, float, char, vv

Và, lớp lưu trữ kiểm soát hai thuộc tính khác nhau của một biến: thời gian tồn tại (xác định thời gian tồn tại của biến) và phạm vi (xác định phần nào của chương trình có thể truy cập nó).

Tùy thuộc vào lớp lưu trữ của một biến, nó có thể được chia thành 4 loại chính:

  • Biến cục bộ
  • Biến toàn cục
  • Biến cục bộ tĩnh
  • Đăng ký biến
  • Bộ nhớ cục bộ của chuỗi

Biến cục bộ

Một biến được xác định bên trong một hàm (được xác định bên trong thân hàm giữa các dấu ngoặc nhọn) được gọi là biến cục bộ hoặc biến tự động.

Phạm vi của nó chỉ giới hạn trong chức năng mà nó được xác định. Nói một cách dễ hiểu, biến cục bộ tồn tại và chỉ có thể được truy cập bên trong một hàm.

Tuổi thọ của một biến cục bộ kết thúc (Nó bị hủy) khi hàm thoát.

Ví dụ 1: Biến cục bộ

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

Biến var không thể được sử dụng bên trong test()và var1 không thể được sử dụng bên trong main()hàm.

Từ khóa autocũng được sử dụng để xác định các biến cục bộ trước đây như:auto int var;

Tuy nhiên, sau C ++ 11 autocó một ý nghĩa khác và không nên được sử dụng để xác định các biến cục bộ.

Biến toàn cầu

Nếu một biến được định nghĩa bên ngoài tất cả các hàm, thì nó được gọi là biến toàn cục.

Phạm vi của một biến toàn cục là toàn bộ chương trình. Điều này có nghĩa là, Nó có thể được sử dụng và thay đổi ở bất kỳ phần nào của chương trình sau khi khai báo.

Tương tự như vậy, vòng đời của nó chỉ kết thúc khi chương trình kết thúc.

Ví dụ 2: Biến toàn cục

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

thú vị bài viết...