[Programming language] The storage period/scope of the variable and the usage of static/extern, staticextern
Storage Class:
Linked Memory Management During Storage Period
For a variable (Different Storage types), you can describe it through the storage period, link attribute, and corresponding scope.
The storage period is the retention time of the variable in the memory. The scope and link of the variable indicate which parts of the program can be used by the variable name. Therefore, there is a failed storage period, Link Attributes, and a combination of scopes.
[1] storage period (the life cycle of a variable, indicating the storage and release time of a variable in the memory space)
Local variable register variable global variable
1) Local variablesThe life cycle of an automatic variable is that the memory space is allocated if the variable is called within the code block. The allocated memory is released when the code block is exited. This idea regards the memory used by the automatic variables as a reusable workspace or temporary memory.
2) Register variableGenerally, variables are stored in the memory, but some variables are also stored in the CPU register or in the cache. As far as possible, the block is accessed and operated, because a register variable exists in a register rather than in the memory, all the addresses that cannot obtain the register variable, but in other respects, the register variable is the same as the automatic variable, that is, they all have code block scopes, empty connections, and automatic storage periods. register variables can be expressed through register.
3) Global variable (static external variable)It has file scopes, external links, and static storage periods. variables are defined outside all functions, that is, an external global variable is created, and it has static variables of external links, that is, it is visible from the declared position to the end of the file. If the program is more clear, you can add extern before the external variable to declare that this variable has, however, we usually do not add extern to declare static variables (global variables) for external links that have been defined in this file ), however, when you want to reference static variables of external links of other files, you must add extern to declare them.
SCOPE/link Property
External link internal link empty Link
1)Variables with code block scope or function prototype scope have empty connections.Which means they are private to the code block or function prototype defined by them.
2)Use externDeclared variables have external Link AttributesIndicates that this variable can be used anywhere in one or more files.
3)Use staticDeclared variables have internal Link AttributesIndicates that this variable can only be used anywhere in this file. Note that the variable declared with static can be the variable in the file scope (the global variable of the internal link) it can also be a local static variable with code blocks. They share a static storage period. Their values are recorded in the memory from one function to the next call, these variables did not disappear when they were completed again. But it is declared as a static variable with a code block scope. It is a null connection property.
Memory Management (where variables are stored in the memory) (important)
Variables can be stored in different places in the memory, depending on their lifecyclesThe lifetime of variables defined outside the function (global variables or static external variables) and static variables defined inside the function is the whole process of program running. The data is stored in the data segment (. DATA) the DATA segment is a fixed-size space for these variables in the memory. It is divided into two parts to store the initialized global variables and static global variables, the other part is used to store the initialized global variables. in the case of an operating system, the uninitialized static global variables (static modified variables) will be initialized to zero by the compiler, their lifetime starts from the execution of the program to the position where the code block is located until the program leaves the code block and acts as a variable (parameter) of the function parameter) the call only exists between the call function cycles and is stored in the stack space. However, it should be noted that static local variables modified by static exist in the memory even though they only have code block scopes, until it is refreshed next time.
Special note
The memory space does not actually store variables, but it can be used to store the data pointed to by variables. Here it also has a pointer meaning,If you assign the result of the malloc () function to a pointer variable type, the pointer variable will contain a dynamically allocated memory address, which is located in a section named "HEAP) in the memory space, the heap is also very small at the beginning, but when the programmer calls malloc or callocWhen a function is assigned, it will increase,Because the heap can share a memory segment with the data segment or stack, the stack overlaps when it exceeds a certain limit.
Sample Code: