C language memory partition and storage type

C language memory partition and storage type

In C language, the storage type of data can change its scope and period. The scope of the variable is different, essentially because of the difference in its storage type. Variables of automatic types and registers belong to dynamic storage methods; while external types and static types belong to static storage methods.

Before understanding the storage type, you need to understand the structure of the process in the memory after the executable file is loaded into the memory:

1. Code area: Store the machine instructions executed by the CPU. The code area is sharable and read-only.

2. Data area: store initialized global variables, static variables (global and local), constant data.

3. BBS area: Uninitialized global variables and static variables are stored.

4. Stack area: automatically allocated and released by the compiler, storing function parameter values, return values ​​and local variables, and real-time allocation and release during program operation. The stack area is automatically managed by the operating system without manual management by the programmer.

5. Heap area: The heap is a memory block allocated by the malloc() function. The free() function is used to release the memory. The application and release of the heap is controlled by the programmer, which is prone to memory leaks.

There are four types of storage:

1. Automatic variables (auto): all non-static local variables in the function. Auto can only be used to identify the storage type of local variables. For local variables, auto is the default storage type and does not need to be explicitly specified. Therefore, the variable identified by auto is stored in the stack area.

2. Static variable (static): The variable with the static keyword before the variable. A variable declared as a static type, whether global or local, is stored in the data area, and its life cycle is the entire program. If it is a static local variable, its scope is within a pair of {}, if it is a static global Variables whose scope is the current file. If static variables are not initialized, they are automatically initialized to 0. Static variables can only be initialized once.

3. External variables (extern): generally used as an extension of the scope of global variables. extern is used to declare global variables defined in other files in the current project that are referenced in the current file. If the global variable is not initialized, it will be stored in the BBS area, and its value will be automatically assigned to 0 when compiling. If it has been initialized, it will be stored in the data area. Global variables, whether initialized or not, their life cycle is the entire program running process. In order to save memory space, when using extern in the current file to declare global variables defined in other files, memory space will not be allocated for them. .

4. Register type (register): Variables that are generally used frequently (such as a variable that needs to be calculated thousands of times) can be set as register variables. Register variables will be stored in registers. The calculation speed is much faster than those stored in memory. Non-register variables. After the variables declared as register are transferred from the memory to the CPU registers, they will reside in the registers of the CPU. Therefore, accessing the register variables will greatly improve the efficiency because the process of transferring variables from the memory to the registers is omitted. Several instruction cycles.

Guess you like

Origin blog.csdn.net/apple_52246384/article/details/110261610