The C program is always composed of the following parts:
(1) Stack
The compiler automatically assigns release management. Local variables and the address returned during each function call, as well as the caller's environment information (such as some machine registers) are stored in the stack. The newly called function automatically allocates storage space for it and temporary variables on the stack. By using the stack in this way, the C function can be called recursively. Each time a recursive function calls itself, a new stack frame is used. Therefore, the variable set in one function call instance does not affect the variables in another function call instance.
A. Local Variables
B. Return address for function calling
C. Caller's environment information (for example, some machine registers)
(2) Heap
It must be assigned to the programmer for release management. If the programmer does not release the program, it may be recycled by the OS at the end of the program. It is usually used for dynamic storage allocation in the heap.
For example, functions such as malloc, calloc, and realloc in the program are allocated from this. Heap is allocated from bottom up.
(3) Non-initialized data segments
This section is usually called the BSS section. This name is derived from an operator of the early assembler, which means "block started by symbol (Block starting with the symbol )", uninitialized global variables and static variables are stored here. Before the program starts to run, the kernel initializes this segment to 0. Description outside the function: longsum [1000]; stores this variable in non-initialized data segments. When the growth of BSS data or stack consumes the free memory allocated to the process by the system, the process will be blocked, the operating system reschedule the operation with a larger memory module. the non-initialized data (BSS) area is used to store static variables of the program. The memory is initialized to zero.
The initialization data area is used to store the initialization data in the executable file.
These two zones are collectively referred to as data zones.
A. uninitialized global variables
B. uninitialized static variables
Static variables are classified into static local variables and static global variables.
(4) initialized data
This section is usually called a data segment, which contains the variables in the program that need to be assigned an initial value. The initialized global variables and static variables are stored here. For example, description other than any function in the C program: intmaxcount = 99; stores this variable in the initialization data segment as an initial value.
A. initialized global variables
B. initialized static variables
Static variables are classified into static local variables and static global variables.
Parts 3 and 4 are collectively referred to as static zones
(5) text section
The machine commands executed by the CPU. Generally, the body segment can be shared, so even if it is a regular environment pointer environment, the Environment string execution program of the table (such as text editing program, C Compilation Program, s h e l, etc) there is only one copy in the memory. In addition, the body segment is often read-only to prevent the program from modifying its own commands due to accidents.
It can be noted that the content of uninitialized data segments is not stored in program files on the disk, because they are all set to 0 before the program starts running. Only the body and initialized data segments must be stored in the program file.
This section is different from the memory ing of p143 In the first-stage textbook. In the textbook, the. Data and. BSS segments are also loaded from the executable file.
Note: The SIZE Command reports the length of the body segment, data segment, and BSS segment (unit: bytes)