The scope, life cycle and memory allocation of variables in C language

Directory

1. Scope

2. Life cycle

3. The relationship between scope and life cycle

4. Memory allocation

1. Scope

Scope is the range in which variables can be accessed. The scope of variables can be divided into the following four types:

1. Process scope (global):

Can be accessed anywhere in the current process;

2. Function scope:

When the process is transferred to a function, it is accessible within its opening and closing curly braces;

3. Block scope:

A block is a code area surrounded by curly braces “{}”, and variables defined in the block have block scope. block scope is visible from the point of definition to the end of the block containing the definition;

4. File scope:

Can be accessed within the current source file.

Note:

The scope of local variables is between “{}” of the current function, and the scope of global variables is the entire current source file or project.

2. Life cycle

The life cycle is the time from the creation of the variable to the destruction of the variable.

Lifecycle of each variable:

1. Global variables:

Created at the beginning of the process and destroyed at the end of the process. After the code is compiled and linked, its initial value is directly written into the executable file, and it is assigned according to the initial value at the time of definition when it is created;

2. Local variables and parameter variables:

Created when entering a function and destroyed when exiting a function;

3. Static variables (static local variables and static global variables) static

(1) Static local variables:

Using static to modify a variable in a function, its life cycle will be the same as that of a global variable, and its scope is limited to the function.

The characteristic is that it has memory capability and will not be initialized every time it is declared. This function is very convenient when used to realize the counting function, for example:

void cnt(void)

{

    static int num = 0;

    num++;

}

In the function, the variable num is a static local variable, which is declared when entering the function for the first time, and then performs the self-increment operation. When entering the function for the second time, num will not be reinitialized to 0, but will remain 1. Then self-increment, its scope is still in the cnt function body.

(2) Static global variables:

The life cycle is the same as global variables, but the scope is limited to the definition file, and extern cannot be used to use it in other source files.

The advantage of static global variables is to enhance the security and robustness of the program, while improving portability. If for a variable, it is assumed that other files are not expected to have the ability to modify this variable, but other files need the value of the variable, you can define a static global variable in the source file and use a function to modify its value at the same time and acquisition, only the function interface is provided externally, and other files use the variable indirectly through the function interface.

Note:

(1) Static global variables are only visible in this file, so other files can also define static local variables with the same name. But it is not conducive to the readability and maintainability of the program, and it is easy to confuse the development.

(2) In addition to modifying variables, the static keyword can also be used to modify functions, so that the functions are only visible in this file and cannot be called by other files.

The lifetime is determined by the variable’s location in memory. Different storage locations, different functions, and different life cycles.

Note: The problem of the same variable name

1. Local variables in different functions can have the same name without affecting each other; global variables cannot have the same name;

2. When a local variable and a global variable have the same name, the local variable will overwrite the global variable.

3. The relationship between scope and life cycle

The two are not actually related

1. Scope is the regulation on whether variables are visible at the grammatical level, which can be understood as a spatial dimension;

2. The life cycle is the time when variables at the binary level exist in memory, which can be understood as the time dimension.

four, memory allocation

In C language, memory is divided into stack area (stack), heap area (heap), uninitialized global data area, initialized global data area, static constant area (static), and code area (data).

1. Stack:

Store function parameters and local variables, and automatically release the space when the function returns. First-in, last-out, increasing toward address decreasing direction (from high address to low address).

Stack features:

(1) Automatic allocation and automatic recovery at runtime: the stack is managed automatically, and the programmer does not need manual intervention. Convenient and simple.

(2) Reusability: The stack memory is actually that space in the program, and the program uses this space repeatedly.

(3) Legacy: due to repeated use of stack memory, the program will not clean up after each use, so when using the stack, it is still the value left in the last stack.

(4) Temporary: The function cannot return the pointer of the stack variable, because this space is temporary.

(5) Overflow: Because the operating system has given the size of the stack in advance, if the stack memory is allocated endlessly in the function, it can always be used up.

2. Heap:

The memory opened by using malloc, realloc, calloc, etc. in C language is on the heap, growing from the direction of increasing address (low address to high address). The freed storage area is manually allocated by the programmer. If the programmer does not release this memory manually, the memory will always be occupied until the program finishes running and is automatically reclaimed by the system.

3. Global zone:

Global variables, static variables (static) (global and local static variables), and string constants (cannot be modified at will) (const-modified global variables are also stored in the constant area, const-modified local variables are still on the stack).

Data segment (data segment), bss segment

(1). Data segment (rw segment) storage: global variables initialized to non-zero, static local variables initialized to non-zero

(2). The bss segment (also known as the ZI segment (zero initial)) stores: global variables that have not been initialized or initialized to 0; local variables that have not been initialized static modification.

4. Constant area:

When compiling, it is encoded in the instruction code in the form of address. For example, string constants and const-modified global variables.

Code segment, rodata segment (read-only data segment)

(1) Corresponding to the code (function) in the program, the code segment is also called the text segment (.text)

(2) The rodata section is often used to store constant data. Rodata is also called a read-only section. The read-only data section can only be read but not written during program running. For example, constants modified by const may be stored in the rodata section.

A few example pictures for assisting understanding: