We need to know that variables are just abstract names of memory addresses. In statically compiled programs, all variable names will be converted into memory addresses during compilation. The machine does not know the names we choose, but only knows the addresses. The use of memory is one of the important factors to be considered in program design, not only because the system memory is limited (especially in embedded systems), but also because memory allocation will directly affect the efficiency of the program. Therefore, we need to have a systematic understanding of memory management in C language. In C language, four memory intervals are defined: code area; global variable and static variable area; local variable area, i.e. stack area; dynamic storage area, i.e. heap area; as follows: 1> Stack area (stack) - automatically allocated and released by the compiler, storing function parameter values, local variable values, etc. Its operation method is similar to the stack in the data structure. 2> Heap area (heap) - generally allocated and released by the programmer. If the programmer does not release it, it may be reclaimed by the OS when the program ends. Note that it is different from the heap in the data structure, and the allocation method is similar to the linked list. 3> Global area (static area) (static) - global variables and static variables are stored together, initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. - After the program ends, they are released by the system. 4> Constant area - constant strings are placed here. After the program ends, they are released by the system. 5> Program code area - binary code of the function body is stored. Let's look at the picture:
First of all, we must know that the source code is compiled into a program, and the program is placed on the hard disk, not in the memory! It will only be called into the memory when it is executed! Let's take a look at the program structure. ELF is the main executable file format of Linux. ELF files are composed of 4 parts, namely ELF header, program header table, section, and section header table. The details are as follows: 1> Program header describes the location and size of a segment in the file, as well as its location and size after it is placed in the memory. That is, the information to be loaded; 2> Sections stores the information of the object file, from the connection point of view: including instructions, data, symbol table, relocation information, etc. In the figure, we can see that Sections include: text text node to store instructions; rodata data node readonly; data data node readable and writable; 3> Section header table contains information describing file sections. Each section has an entry in this table; each entry gives the name, size, and other information of the section. Equivalent to an index! How is the program distributed when it is loaded into memory? Let's look at the picture above: 1 The text, initialized data and uninitialized data are what we call the data segment, and the text is the code segment; 2> Above the text segment is the constant area, and above the constant area are the global variables and static variables area, which occupy the initialized data and uninitialized data; 3> Above it is the heap, the dynamic storage area, which is growing upward; 4> Above the heap is the stack, which stores local variables, that is, after the code block where the local variables are located is executed, this memory will be released, and the stack area here is growing downward; 5> Command line parameters are 001 and the like, and environment variables have been discussed in previous articles. If you are interested, you can go and have a look. We know that memory is divided into dynamic memory and static memory. Let's talk about static memory first.