C memory layout (36)

        We talked about the stack area, heap area and static storage area in the previous section , so let's take a look at the general layout of the program file . Let's first take a look at the correspondence between different codes in executable programs, as shown in the following figure

picture.png

        We see that the initialized global variables and the statically modified local variables in the program are placed in the .data segment, the uninitialized global variables and the statically modified local variables are placed in the .bss segment, functions and Function calls are placed in the .text section.

        Let's take a look at the difference between a program and a process. A program is a static concept that is represented as an executable file, while a process is a moving image concept. The program is loaded and run by the operating system to get the process. Each program can correspond to multiple processes, but each process can only correspond to one program . For example, a browser is equivalent to a program. If it is not running, there is no corresponding process. But we can open the same browser multiple times at the same time, and each open browser is equivalent to a process.

        We sometimes get this question in interviews: Is a text file containing script code a type of executable program? If so, what kind of process does it correspond to? What we are thinking of is a script program, so it can be run directly, so what kind of process will it correspond to? No idea. Haha, let's take a look at the difference between general executable programs and script files, as shown in the following figure

picture.png

        We see that the executable program is directly loaded by the operating system, which directly corresponds to a process. But after we run the script file, there will be a corresponding script interpreter in the operating system. After the script interpreter is executed, a corresponding process will be generated, which in turn reads and interprets the execution script . This will run the script file.

        Let's take a look at the mapping of the file layout in memory

picture.png

        We see that the executable program a.out is divided into header files, .text, .data and .bss sections . Correspondingly, the header file is removed in the process and the stack and heap are added. Then let's talk about the role of each segment. The stack segment officially exists after the program runs, which is the basis for the program to run; the .bss segment stores uninitialized global variables and static variables; the .text segment stores the program The executable code of the .data section stores the initialized global variables and static variables; the .rodata section stores the constant values ​​in the program, such as string constants.

        The static storage area usually refers to the .bss and .data sections in the program; the read-only storage area usually refers to the .rodata section in the program; the space occupied by local variables is the space on the stack; the dynamic space is the space in the heap; the program is executable The code is stored in the .text section.

        So let's think about it, both global variables and static variables, why do initialized and uninitialized variables need to be stored in different segments? Usually in the compiler, we have to initialize the variables that are not initialized to 0. If we check one by one, is it inefficient? If we store them separately, those variables that have been initialized do not need to be checked, so the efficiency will be higher.

        Through the study of the memory layout in the program file, the summary is as follows: 1. The program source code corresponds to different storage areas in the executable program after compilation; 2. The program is different from the process, the program is a static concept, and the process is a dynamic diagram concept; 3. The stack segment is the basis of program operation and exists only in the process space; 4. The executable code of the program is stored in the .text segment, which is read-only; 5. The .bss and .data segments are used to save global variables and static variables.


        Welcome to learn C language together , you can add me QQ: 243343083 .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324979747&siteId=291194637