Go C + + Scope explanation

Source: Internet
Author: User

Original address: http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777433.html

Scope rules tell us the valid range of a variable, where it was created and where it was destroyed (that is, out of scope). The valid scope of a variable starts at its defining point, to the first closing parenthesis of the nearest opening brace pair before and after the variable is defined. That is, the scope is determined by the closest pair of parentheses where the variable resides.

(1) Global variables:

Global variables are defined outside the body of all functions, and the part of the program (and even the code in other files) can be used. The global variable is not affected by the scope (that is, the lifetime of the global variable continues until the end of the program). If you use the extern keyword in one file to declare a global variable that exists in another file, the file can use this data.

(2) Local variables:

Local variables appear within a scope, and they are confined to a function. Local variables are often referred to as automatic variables because they are automatically generated when they enter the scope and disappear automatically when they leave the scope. The keyword Auto can explicitly describe the problem, but the local variable defaults to auto, so it is not necessary to declare it as auto.

(3) Register variable

A register variable is a local variable. The keyword register tells the compiler to "access this variable as quickly as possible." Speed of access depends on reality, but, as the name implies, this is often done by placing variables in registers. This does not guarantee that it will be placed in registers or even improve access speed. This is just a hint to the compiler.

There is a limit to using the Register variable: (1) It is impossible to obtain or calculate the address of the register variable; (2) Register variables can only be declared in one block (there can be no global or static register variable). However, you can use the register variable as a formal parameter in a function (that is, in the parameter table).

In general, you should not speculate on the compiler's optimizer, because it might do better than we do. Therefore, it is best to avoid using the keyword register.

(4) Static variables

The keyword static has some unique meanings. In general, a function defines a local variable that disappears at the end of a scope in a function. When this function is called again, the storage space of the variable is recreated and its value is reinitialized. If you want to make the value of a local variable persist throughout the lifetime of the program, we can define the local variable of the function as static, and give it an initial initialization. Initialization is performed only when the function is first called, and the value of the variable between function calls remains constant, in this way the function can "remember" some pieces of information between the function calls. This is called a static local variable , has a local scope, it is initialized only once, since the first time it was initialized until the end of the program run, it is different from the global variable is that the global variable is visible to all functions, Static local variables are always visible only within the body of the function in which they are defined.

We may wonder why we don't use global variables. The advantage of a static local variable is that it is not available outside the scope of the function, so it cannot be easily changed. This causes the error to be localized.

There are also static global variables , with global scope, which differs from global variables in that if a program contains multiple files, it acts on the file that defines it and does not work in other files, that is, variables modified by the static keyword have a file scope. This way, even if two different source files define static global variables of the same name, they are also different variables.

(5) External variables

extern tells the compiler that there is a variable and function, even if the compiler does not see it in the current file. This variable or function may be defined in a file or later in the current file. For example, extern int i; The compiler will know I must exist somewhere as a global variable. When the compiler sees the definition of the variable i and does not see any other declarations, I know that it has found the same declared I in front of the file.

(6) Const constant

Const tells the compiler that the name represents a constant, both internal and user-defined data types can be defined as const. If you define an object as a constant and then try to change it, the compiler will generate an error. In C + +, a const must have an initial value.

(7) volatile variables

The qualifier Const tells the compiler that "this is not going to change" (this is to allow the compiler to perform additional optimizations), while the qualifier volatile tells the compiler to "do not know when to change" to prevent the compiler from making any optimizations based on the stability of the variable.

From allocating memory space: global variables, static local variables, static global variables allocating space in static stores, and local variables allocating space in stacks

Go C + + Scope explanation

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.