(2) Memory Allocation
Two commonly used languages when describing variables in C Language
1. Scope: Also called visible domain, refers to the scope of the variable. In which range, the variable is visible and usable.
2. Survival: it is also called the storage period. It refers to the survival period of the variable from creation to destruction.
The scope and the existing domain are two different concepts. For example, a variable exists at a certain position in the program (the address is allocated in the memory) but is invisible (unavailable ).
Scope
From the scope perspective, there are three types of variables:
1. Global Variables
In C, variables declared outside of any function are called global variables. Generally, global variables are visible anywhere. Of course, there are exceptions. For example, if a local variable with the same name is declared in the statement block {}, the global variable will be invisible for the moment.
// Global variable int Foo = 5; void fun () {printf ("Fun ()-> FOO... % d \ n ", foo);} int main () {int bar = 3; printf (" Main ()-> FOO... % d \ n ", foo); printf (" Main ()-> bar... % d \ n ", bar); {// local variable, shielding the global variable fooint Foo = 6; printf (" {}-> FOO... % d \ n ", foo); // local variable, blocking local variable barint bar = 4; printf (" {}-> bar... % d \ n ", bar) ;}fun (); Return 0 ;}
Run
The running result shows the visible range of global variables. Of course, if you try to use a global variable before it is declared, an error will also occur. (This is related to the rule "not declared, unavailable .)
In addition, the design idea in C is: a global variable is also a default external variable (extern ). That is to say, a global variable is not only globally visible in this file, but also visible in other files. For example
//1.cint foo;
In another file
//2.cint foo;
Compilation error: foo is repeatedly defined and has a name conflict.
This default behavior has been widely criticized. Many people think that the visible field of variables should be limited to the current file by default, and should be controlled by the programmer when the file needs to be expanded. In addition, functions have the same default behavior.
How can this behavior be prevented?
(1) If a variable with the same name is required, you can modify the existing global variable with the same name as static to make it a static global variable. In this way, its visible domain is limited to its file. For example
// 1. cstatic int Foo; // The visible domain is restricted in this file.
Re-declare int Foo; in another file.
(2) Use the global variables in other files. The syntax is extern int Foo; the keyword extern indicates that the variable Foo has been defined elsewhere. Here, it is just a declaration before use, not a repeated definition. Of course, not making this statement is also unavailable, which once again reflects: visible, but not necessarily available.
2. Static variables in the file
This is the static global variable mentioned above.
3. Local Variables
In a function, or more directly, a variable defined in the statement block {} is a local variable. Its visible fields are limited to statement blocks and cannot be referenced elsewhere. When a function is called, local variables are allocated to the storage zone by the system. variables with the same name in different functions actually occupy different units in the memory, therefore, you can define local variables with the same name in different functions. For example, the main function defines local variables.
Summarize the scopes
Three scopes exist in C Language
(1) block Scope
Automatic variables (Auto, register) and internal static variables (static) have block scopes. For variables declared in a block, their scopes start from the declaration point and end until the block ends. The parameter declared in the function definition has a limited scope in the function body. It is not the same as the variable with the same name declared in other functions. The same variable name can be used in different functions, the compiler will allocate different storage units for these variables without conflict.
(2) File Scope
The external static variable has a file scope. From the Declaration point to the end of the file, the file referred to here is the basic unit of compilation-C file.
(3) Global (Program) Scope
A global variable (extern) has a global scope. As long as it is declared before use, global variables can be used anywhere in the Program (composed of several files.
There are three types of survival periods:
1. Static variables)
Global variables and specified static local variables both have a static storage period. They exist from the beginning of the program to the end of the program, so they are collectively referred to as static variables.
2. Auto variable)
The local variables and register variables (register variable) that are not specified as static are both automatic variables. The parameters of the function and the variables defined in the Code block belong to the auto variable, which is the most widely used variable in C language. These variables are Stack-allocated and are dynamically allocated to the storage space. For example, when a function is called, a bucket is allocated to the parameter. When the function call ends, the bucket is automatically released. When the variables defined in the Code block (including the variables defined in the function) are executed in the variable declaration statement, the system allocates space for these auto variables. When the program flow leaves the code block, these variables are automatically revoked and the occupied memory space is released.
3. Heap Variables
The variables allocated to the memory area are stored in the heap through the malloc () function, which is called the heap variable. In addition, it seems that this "heap" has nothing to do with the heap in the data structure. It is just a name. This variable needs to manually release the memory area: Free (variable name), that is, it exists from the time of creation until free () is released. Of course, even if you forget to release it, the operating system will release the memory allocated to it after the program ends. However, we recommend that you create and release instances.