C LanguageProgramMemory Layout
I. Storage Area of C language programs
After a program written in C language is compiled with links, a unified file is formed, which consists of several parts and several other parts will be generated when the program is running, each part represents a different storage region:
1.CodeSegment (code or text ):
A code segment consists of machine codes in the program. In C, program statements are compiled to form machine code. During program execution, the program counter of the CPU points to each piece of code in the code segment and runs in sequence by the processor.
2. read-only data segment (RO data ):
Read-only data segments are data that will not be changed by the program. These data segments are similar to table-based operations. Because these variables do not need to be changed, they only need to be placed in read-only memory.
3. read/write data segments initialized (RW data ):
Initialized data is declared in the program and has initial values. These variables need to occupy the storage space. During program execution, they need to be located in the read/write memory area, and has an initial value for reading and writing when the program is running.
4. read/write data segments not initialized (BSS ):
Uninitialized read/write data is declared in the program, but there are no initialized variables. These variables do not need to occupy storage space before the program runs.
5. Heap ):
Heap memory only appears when the program is running. Generally, it is allocated and released by programmers. If a programmer does not release an operating system, the operating system can reclaim the memory after the program ends.
6. STACK ):
Stack memory only appears when the program is running. The variables used inside the function, function parameters, and returned values use the stack space, which is automatically allocated and released by the compiler.
Memory layout of the C language target file
Code segment, read-only data segment, read/write data segment, uninitialized data segment belongs to the static region, while heap and stack belong to the dynamic region. Code segments, read-only data segments, and read/write data segments are generated after connection. uninitialized data segments are opened during program initialization, and stacks are allocated and released during program running.
The C language program is divided into two states: Image and runtime. The compiled and connected images only contain code segments, read-only data segments, and read/write data segments. Before running the program, uninitialized data segments are dynamically generated, and heap and stack regions are dynamically formed when the program is running.
In general, in a static image file, each part is called a section, and each part at runtime is called a segment.
II. C language program section
1. Code segment)
Code segments are generated by various functions. Each function statement is compiled and compiled to generate binary machine code (the compiler determines the specific system structure of the machine code ).
2. read-only data segment (RO data)
The read-only data segment is generated by the data used in the program. This data segment does not need to be changed during running. Therefore, the compiler puts this data segment into the read-only part. The read-only global variables, read-only local variables, and constants used in the program are stored in the read-only data zone during compilation. Note:
Define the global variable const char a [100] = {"abcdefg"}. a read-only data zone of 100 bytes is generated and initialized using "abcdefg. If it is defined as: const char a [] = {"abcdefg"}, an 8-byte read-only data segment (and '\ 0') is generated based on the string length '), therefore, full initialization is generally required in read-only data segments.
3. read/write data segments (RW data)
Read/write data segments represent a part of the data areas that can be read or written in the target file. In some cases, they are also called initialized data segments, similar to read-only data segments, they all belong to static areas in the program, but are writable. Generally, initialized global variables and local static variables are placed in the read/write data segment, for example, defining static char B [100] = {"abcdefg"} in the function "}; the read/write data zone must be initialized in the program. If it is defined only and has no initial value, the read/write data zone will not be generated and will be positioned as the uninitialized data zone (BSS ).
If a global variable (a variable defined outside the function) is added with static modification, this indicates that the global variable can only be used in the file and cannot be used by other files.
4. Data Segment not initialized (BSS)
Similar to the read/write data segment, it also belongs to the static data zone, but the data in the segment has not been initialized. Therefore, it will only be identified in the target file, instead of a segment in the target file, which will be generated at runtime. The uninitialized data segment is generated only during the initialization phase, so its size does not affect the size of the target file.
Note the following points when using variables in C language programs:
1. the variables defined in the function body are usually on the stack and do not need to be managed in the program. They are processed by the compiler.
2. The memory allocated by functions such as malloc, calloc, and realloc is on the heap. The program must ensure that free is used to release the memory. Otherwise, the memory will leak.
3. All functions define global variables in vitro. Variables with static values are placed in the global zone, both inside and outside the function.
4. Use the variables defined by const to store in the read-only data zone of the program.
Iii. Middle-stage Application
The following is a simple example to illustrate the correspondence between variables and segments in C language. The global zone (static zone) in the C language program corresponds to the following segments: RO data; RW data; BSS data.
Generally, the directly defined global variable is in the uninitialized data zone. If the variable is initialized, it is in the initialized data zone (RW data), and the const is placed in the read-only data zone.
Example: const char Ro [] = {"this is read only data"}; // read-only data Zone
Static char rw_1 [] = {"this is global read write data"}; // initialized read/write data segment
Char bss_1 [100]; // uninitialized data segment
Const char * ptrconst = "constant data"; // put the string in the read-only data segment
Int main ()
{
Short B; // occupies 2 bytes on the stack
Char A [100]; // 100 bytes are opened on the stack. The value of the parameter is its first address.
Char s [] = "abcdefg"; // s occupies 4 bytes on the stack
// "Abcdefg" is stored in the read-only data storage area, which occupies 8 bytes.
Char * P1; // P1 occupies 4 bytes on the stack
Char * P2 = "123456"; // P2 is on the stack. The content indicated by P2 cannot be changed,
// "123456" is in the read-only data Zone
Static char rw_2 [] = {"This is local read write data"}; // partially initialized read/write data segments
Static char bss_2 [100]; // partial uninitialized data segment
Static int C = 0; // global (static) initialization Zone
P1 = (char *) malloc (10 * sizeof (char); // allocate the memory area in the heap Area
Strcpy (P1, "XXXX"); // "XXXX" is placed in the read-only data zone, which occupies 5 bytes
Free (P1); // use free to release the memory pointed to by P1
Return 0;
}
The read/write data segment contains the initialized global variable static char rw_1 [] and the local static variable static rw_2 []. the difference is that during the compilation, it can be used in the email department or in the entire file. Rw_1 [] will be placed in the read/write data zone regardless of whether there is static modification, but whether it can be referenced by other files or not. The latter is different. It is a local static variable placed in the read and write data area. If there is no static modification, its meaning changes completely, it will be a local variable opened up in the stack space, rather than a static variable. Here rw_1 [], rw_2 [] does not have a specific value, indicating that the size of the static zone is determined by the length of the subsequent strings.
For 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, it is used only within the function. The value after Initialization is not set for the uninitialized data segment. Therefore, the value must be used to specify the size of the region. The editor sets the length to be increased in BSS based on the size.
Stack space is mainly used for the storage of the following three data:
1. dynamic variables in the function
2. Function Parameters
3. function return value
Stack space is dynamically opened and recycled. In the process of function calling, if there are many layers of function calling, the required stack space will gradually increase. For parameter passing and return values, if a large struct is used, the stack space used will also be relatively large.