Teach you to layout the memory of C programming program Focus on the following: The composition of various segments of C language program in memory Features and Common Mistakes in C Language Program Connections The way C programs run An executable program (binary file) is formed from a C language code (text file) and needs to undergo three stages of compilation-assembly-connection. The compilation process generates a assembler program from a C language text file. The assembler process forms the assembler program into binary machine code, and the linking process combines the binary machine code files generated from each source file into a single file. After a program written in C language is compiled and linked, it will form a unified file, which consists of several parts. When the program is running, it will generate several other parts. Each part represents a different storage area: 1. Code segment (Code or Text) The code segment consists of the machine code that is executed in the program. In C language, after the program statement is compiled, machine code is formed. During the execution of the program, the CPU's program counter points to each piece of machine code in the code segment and is run by the processor in turn. 2. Read-only data segment (RO data) Read-only data segments are data that the program uses that will not be changed. The use of these data is similar to look-up table operations. Since these variables do not need to be changed, they need only be placed in read-only memory. 3. Initialized read and write data segments (RW data) The initialized data are variables declared in the program and have initial values. These variables need to occupy the space of the memory. They need to be located in the readable and writable memory area when the program is executed, and have initial values ​​to be read by the program during runtime. write. 4. Uninitialized data segment (BSS) Uninitialized data is declared in the program, but there are no initialized variables. These variables do not need to occupy memory space before the program runs. 5. Heap Heap memory occurs only when the program is running, and is typically allocated and released by the programmer. In the case of an operating system, if the program is not released, the operating system may reclaim the memory after the program (for example, a process) ends. 6. Stack The stack memory appears only when the program is running. The variables used inside the function, the parameters of the function, and the return value will use the stack space. The stack space is automatically allocated and released by the compiler. Memory layout of a C language object file See an example: Int a = 0; // The global initialization area,. Data section staTIc int b=20; // Global initialization area. Data section Char *p1; // global uninitialized area .bss section Const int A = 10; //.rodata section Void main(void) { Int b; // stack Char s[] = "abc"; //Stack Char *p2; //Stack staTIc int c = 0; // global (static) initialization area .data section Char *p3 = "123456"; //123456\0 is in the constant field, p3 is on the stack. P1 = (char*) malloc(10);// The allocated 10 and 20 byte area is in the heap area P2 = (char*) malloc(20); Strcpy(p1, "123456"); //123456\0 In the constant area, the compiler may optimize it to a place with the "123456" pointed to by p3 } Code segments, read-only data segments, read-write data segments, and uninitialized data segments are static regions, and heaps and stacks are dynamic regions. Code segments, read-only data segments, and read-write data segments will be generated after the link. Uninitialized data segments will be opened when the program is initialized, and the heap and stack will be allocated and released during program execution. The C language program is divided into two states: image and runtime. In the compile-connected image, only the code section, read-only data section (RO Data), and read-write data section (RW Data) will be included. Before the program runs, the uninitialized data segment (BSS) will be dynamically generated. During the execution of the program, the Heap area and the Stack area will also be dynamically formed. In general, in a static image file, each section is called a section (SecTIon), and each section at runtime is called a section. If not distinguished in detail, they can be collectively referred to as segments. After C language is compiled and connected, it will generate code segment, read-only data segment (RO Data) and read-write data segment (RW Data). At runtime, in addition to the above three areas, an uninitialized data segment (BSS) area and a Heap area and a Stack area are included. 1. Code segment (code or text) The code segment is generated by each function. Each statement of the function will be finally compiled and compiled to generate binary machine code (which machine system code is determined by the compiler). 2. Read-only data segment (RO Data) The read-only data segment is generated from the data used in the program. The feature of this part of the data is that it does not need to be changed during operation, so the compiler will put the data segment into the read-only part. Read-only global variables in the C language, read-only local variables, constants used in the program, etc. will be put into the read-only data area at compile time. Note: The global variable const char a[100]={"ABCDEFG"} is defined; a read-only data area of ​​size 100 bytes will be generated and initialized with "ABCDEFG". If it is defined as: const char a[ ]={"ABCDEFG"}; 8 bytes of read-only data segments (also '\0') are generated based on the length of the string, so in the read-only data segment, generally Need to do a full initialization. 3. Read and write data segments (RW Data) The read and write data segments represent a portion of the target file that can be read or written. In some cases they are also referred to as initialized data segments. This portion of data segments and code segments, like read-only data segments, are Static area in the program, but it is writable. Usually initialized global variables and local static variables are placed in the read and write data segments, such as: Defined in the function staTIc char b[100]={"ABCDEFG"}; Read and write data area is characterized by the program must be initialized If it is only defined and there is no initial value, the read/write data area will not be generated and it will be positioned as the uninitialized data area (BSS). If a global variable (a variable defined externally to the function) is statically decorated, this means that it can only be used within a file and cannot be used by other files. 4. Uninitialized data segment (BSS) Similar to reading and writing a data segment, it also belongs to the static data region, but the data in this segment is not initialized. Therefore, it will only be identified in the target file, and it will not really be called a segment in the target file. This segment will be generated at runtime. Uninitialized data segments are only generated during the initialization phase of the run, so its size does not affect the size of the target file. In the C language program, the following points need to pay attention to the use of variables: 1. The variables defined in the function body are usually on the stack and do not need to be managed in the program, and are processed by the editor. 2. The memory allocated by the functions of malloc, calloc, realloc, etc. to allocate memory is on the heap. The program must be guaranteed to use free release, otherwise a memory leak will occur. 3. All the functions defined in the body are global variables, and variables that are added after static are placed in the global area either inside or outside the function. 4. Use const defined variables will be placed in the read-only data area of ​​the program. The following uses a simple example to illustrate the correspondence between variables and segments in the C language. The global area (static area) in the C language program actually corresponds to the following sections: RO Data; RW Data; BSS Data. In general, a directly defined global variable is in the uninitialized data area. If the variable is initialized, it is in the initialized data area (RW Data), and const is placed in the read-only data area. Const char ro[ ] = { "this is read only data"}; //Read-only data area Static char rw_1[ ] ={"this is global read write data"}; //The data segment has been initialized Char BSS_1[ 100]; //uninitialized data segment Const char *ptrconst = "constant data"; //String puts only read data segment Int main() { Short b; //On the stack, occupying 2 bytes Char a[100]; //Open up 100 bytes on the stack, its value is its first address Char s[ ]=“abcdefgâ€; //s is on the stack, occupies 4 bytes, “abcdefg†itself is placed in the read-only data storage area, accounting for 8 bytes Char *p1; //p1 is on the stack and occupies 4 bytes Char *p2="123456"; //p2 On the stack, the contents pointed to by p2 cannot be changed, "123456" is in the read-only data area Static char rw_2[ ]={"this is local read write data"};//Locally initialized read and write data segments Static char BSS_2[100]; //Local uninitialized data segment Static int c = 0; // global (static) initialization area P1 = (char *) malloc (10 * sizeof (char)); / / allocate memory area in the heap area Strcpy(p1, "xxxx"); //"XXXX" is placed in the read-only data area, accounting for 5 bytes Free(p1); //free to release the memory pointed to by p1 Return 0; } The read-write data segment contains the global variables static char rw_1[] and the local static variable static rw_2[]. The difference is whether the compilation is used inside the function or it can be used throughout the file. For rw_1[] with or without static decoration, it will be placed in the read-write data area, just whether it can be referenced by other files or not. The latter is not the same, it is a local static variable, placed in the read-write data area. If there is no static modification, its meaning is completely changed. It will be a local variable opened in the stack space, instead of a static variable, here After rw_1[],rw_2[] has no specific value, it indicates that the size of the static zone is the same as the length of the following string. For the uninitialized data areas BSS_1[100] and BSS_2[100], the difference is that the former is a global variable and can be used in all files; the latter is a local variable and is used only within the function. The uninitialized data segment does not set the following initialization value, so the size of the specified region must be specified using a numerical value. The editor will set the length to be increased in the BSS according to the size. The stack space is mainly used for the storage of the following 3 data: 1. Dynamic variables inside a function 2 function parameters The return value of the function Stack space is dynamically opened up and recycled. In the function call process, if the level of the function call is more, the stack space needed is gradually increased. For the parameter passing and return values, if a larger structure is used, the stack space used will be larger. Product categories of Stylus Pen For Huawei, which is just for Huawei M5, M6, C5, Matebook E, other tablets can not be used. Pls check with your huawei tablet model before ordering. We have advanced production equipment, strong software and hardware development capabilities. We have the perfect after-sale service and technical support. Looking forward to our cooperation. Stylus Pen For Huawei,Surface Pen,Touch Pen,Touch Screen Pen Shenzhen Ruidian Technology CO., Ltd , https://www.wisonen.com