Storage Class and scope of C ++ Variables

Source: Internet
Author: User
Tags variable scope least privilege

Storage Class and scope of C ++ Variables

Summarize the storage class and scope of variables in C ++..

(1) the storage class of the identifier determines the time when the identifier exists in the memory (we can understand that the identifier is the identifier for determining a variable, that is, the variable name we call it)



Ii. Storage category

(1) static storage class: static storage class variable (static variable) exists from the beginning of the program, and its life cycle is accompanied by the whole program.

(2) Automatic Storage category: When a variable is automatically stored, the variable is defined as soon as it enters the program that defines them and leaves the block (scope) where they are located) and then it becomes an automatic variable. The keywords auto and register are used to declare automatic variables,



Iii. Automatic Storage:

(1) only local variables in the function are automatically stored. The variables we define in the MAIN function are generally automatic variables, and the keyword auto can be left blank, by default, local variables are automatic variables (we call automatic storage variables as automatic variables), such:

Int;

Int B;

All are automatic Variables

(2) Automatic variables also have a register variable, which uses the keyword register. We declare the frequently called variables as register variables so that they exist in registers as the name suggests, to achieve faster storage, we can understand this. Many compilers now have the ability to identify frequently used variables without manual setup by programmers.

According to the minimum privilege principle, we should define automatic variables. The code should only be granted with the permission to complete the design task, without more permissions.


Iv. static storage class

(1) The keywords extern and static are used for static storage class variables. static storage class variables (static variables) exist from the beginning of the program, and their life periods are accompanied by the whole program, the execution process of the program has always existed. However, even static variables do not mean that these variables (professionally called identifiers) are usable (or accessible) throughout the program ), A dedicated instance is provided below.
(2) Many static variables are mistaken for being consistent with global variables, which is incorrect.Static variables are only visible within the scope of the variables, while global variables are visible everywhere. This is the difference between static variables and global variables,We need to correct one point: the storage class (storage class) and variable scope are two independent issues.
We should follow the principle of least privilege without using global variables.

(3) Let's explain the difference between global variables and static variables: global variables do not show global variables modified with static, but global variables are static by default, and the scope is the entire project, global variables defined in one file. In another file, global variables can be used through the Declaration of the extern global variable name.

Global static variables are global variables modified with static display, and the scope is the file. Other files cannot be declared with extern.


V. Scope of identifier (variable)

(1)The range of the identifier can be used in the program to become the scope of the identifier, in which we call the identifier as a variable, more professional. For example, if we declare a local variable in a statement block, we can only use this variable in this statement block.

(2) The scope is roughly divided into four categories:

L global scope: this variable is known from its declaration to the end of the file.

L local scope: the variable declared in a statement block. Its scope lies in this statement block.

L function prototype scope: it is in the form parameter list of the function.

The local scope is highlighted here: when the statement block is nested and an identifier in the outer statement block is the same as that in the inner statement block, the identifier in the outer statement block is hidden, until the inner statement block ends.



Note 6:

(1) We should avoid using global variables. Using global variables may result in incorrect access variables when no access is required, which violates the principle of least privilege. Only variables in a specific function should be declared as local variables of that function, rather than global variables.

(2) Avoid using variable names that will be hidden in the external scope. This avoids using the same identifier in the program.

The above summary illustrates the type and scope of the variable. In this case, we will give a program to list the above situations. As follows:

# Include
 
  
Using namespace std; void use_Global (); // declare the void use_Local () function; void use_Static_Local (); int a = 1; // define a global variable int main () {cout <"the value of global a in the main function is"
  <输出全局变量的值,值为 1 int a 在main函数中定义一个同名字的局部变量 cout<<"now ,in the main function , local is "<输出局部变量,此时全局变量被隐藏,此时的输出的值为 10 { 在语句块中定义局部变量 cout<<"int scope ,local "<输出值为:7 } cout<<"local in main's outer is"<脱离语句块,输出变量,输出值为:10 use_local(); 使用函数,函数中定义一个局部变量use_static_local(); 使用函数,函数中定义一个静态局部变量use_global(); 使用函数,函数中使用全局变量use_local();use_static_local();use_global(); return 0;}void use_global(){cout<<"\n global "<此作用域中,没有定义同名的标识符,所以输出的全局变量,值为10cout<<"enter use_global "<
   







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.