Scope and life cycle of c/c++--c++ variables, scope and life cycle of variables in C language

Source: Internet
Author: User

Global variables

Scope: Global scope (global variables can be used for all source files only if they are defined in one source file.) )
Life cycle: Program run time is always present
Reference method: The global variable to be referenced must be declared with the extern keyword in other files.
Memory Distribution: Global Data area
Note: If a global variable of the same name is defined in two files, the connection error: variable redefinition

Global static variables

Scope: File scope (visible only in the file that is defined.) )
Life cycle: Program run time is always present
Memory Distribution: Global Data area
Definition method: Static keyword, const keyword
Note: As long as the files do not contain each other, in two different files can be defined exactly the same two static variables, they are two completely different variables

Static local Variables

Scope: local scope (visible only in local scope)
Life cycle: Program run time is always present
Memory Distribution: Global Data area
Definition method: Local scope with static definition
Note: only initialized once, lock protection is required in multiple threads

Local variables

Scope: local scope (visible only in local scope)
Life cycle: The program runs out of local scope and is destroyed
Memory Distribution: Stack area
Note: The auto indicator identifies

It is also important to note that it is critical to master the use of the static keyword

Tips:

If the global variable is accessed only in a single C file, the variable can be modified to a static global variable to reduce the coupling between modules;
If the global variable is accessed only by a single function, the variable can be changed to the static local variable of the function to reduce the coupling degree between the modules;
When designing and using functions that access dynamic global variables, static global variables, and static local variables, you need to consider the reentrant problem, because they are all placed in a static data store and are globally visible;
If we need a reentrant function, then we must avoid using the static variable in the function (a function called "Internal memory" function)
The static variable condition must be used in the function: for example, when the return value of a function is a pointer type, the address of a static local variable must be the return value, and if it is auto type, it is returned as the wrong pointer. http://blog.csdn.net/yunyun1886358/article/ Scope and life cycle of variables in C language

Rectification Book:

From the storage model, we can see that the models of rectification and can have some correspondence:
Static store, global data area
Dynamic Storage (Stack)
Types of variables:
1. Local variables and global variables
Local variables are also known as internal variables. A local variable is defined within a function as a description. Its scope is limited to functions, leaving the function and then
It is illegal to use this variable.
A global variable is also known as an external variable, which is a variable defined outside the function. It does not belong to a function, it belongs to a source program file. Its
The scope is the entire source program. The use of global variables in functions should generally be described as global variables. Only the global changes that are described within the function
To be used. The descriptor for the global variable is extern. But a global variable, defined before a function, is used within that function to add
To explain.

From the scope of the variables (global scope, local scope, file scope) (that is, from the space) point of view, can be divided into
Global variables (the scope of a static global variable is the range of the file (file-scoped)
Local variables.
From the other angle, it can be divided into static storage mode and dynamic storage mode from the angle of the time (i.e. the lifetime) of the variable value existence.
The visible life cycle is only related to the location where the variable is stored.
Auto variable:
The local variables in the function, such as those that are not specifically declared as static storage classes, are dynamically allocated storage space, number
stored in the dynamic storage area. The parameters in the function and the variables defined in the function (including the variable defined in the compound statement)
The system allocates storage space to the function when it is called, and is automatically released at the end of the function call.
These storage spaces. This type of local variable is called an automatic variable. Automatic variables are declared with the keyword auto as the storage class.
Keyword auto can be omitted, auto does not write is implicitly designated as "Automatic Storage category", which belongs to dynamic storage.
To declare a local variable with static:
Sometimes you want the value of a local variable in a function to remain the original value after the function call ends, and then you should specify that the local variable is "static
Local variable, declared with the keyword static

Register variable:
To improve efficiency, the C language allows local variable values to be placed in registers in the CPU, which are called "register variables" with
Register for the keyword.
1) only local automatic variables and formal parameters can be used as register variables;
2) A computer system has a limited number of registers, can not define any number of register variables;
3) Local static variables cannot be defined as register variables
Declare an external variable with extern:
An external variable (that is, a global variable) is defined outside the function and is scoped to start at the variable definition, to
At the end of this program file. If an external variable is not defined at the beginning of the file, its valid scope is limited to the definition
End of document. If the function that precedes the definition point wants to refer to the external variable, you should use the keyword extern before the reference
Make an "external variable declaration" of the variable. Indicates that the variable is an external variable that has already been defined. With this statement, you can
The external variable is legitimately used from the declaration.
  
Summarize:
From scope to divide into:

-Auto variable auto, i.e. dynamic local variable (leave function, value disappears)
Local variable-static local variable static (leave function, value also remains)
-Register local variable register (leave function, value disappears)
-Formal parameters can be defined as automatic variables or register variables

-Static external variable (scope only in the file, file scope)
Global variables-External variables (non-static external variables, global variables, can be used by other files)

From the life cycle of a variable:
-Automatic variable Auto
Dynamic Storage-Register variable register
-Formal parameters

-Static local variables
static storage-Static external variables
-External variables (global variables)

Where to store the value of the variable:

-Static local variables
Static storage in memory (global data area)-Static external variables
-External variables (global variables)

-Automatic variable Auto
In-Memory dynamic storage (stack)-Formal parameters


Registers in the CPU-register variable register

Local static variable, scope is local, and life cycle is the whole process. Static local variables are static storage, which has the following characteristics: (1) Static local variables are defined within a function, but not as an automatic variable, when called, the Exit function disappears. A static local variable always exists, that is, its lifetime is the entire source program. (2) The lifetime of a static local variable is the entire source program, but its scope is still the same as the automatic variable, that is, the variable can only be used within the function that defines it. After exiting the function, it cannot be used even though the variable continues to exist. Http://blog.sina.com.cn/s/blog_80ce3a550100yl1r.html

Scope and life cycle of c/c++--c++ variables, scope and life cycle of variables in C language

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.