Tag: Recycle determines it's this ACK Snippet phase archive link
Memory layout for C language programs
One: Storage area of C language Program
C Language Program programmed Yi-Link, will form a unified file, it consists of several parts, in the program run will produce several other parts, each part represents a different storage area:
1. Snippet (code or TEXT):
The code snippet consists of the machine code in the program. In the C language, the program statements are compiled to form machine code. During the execution of the program, the CPU's program counter points to each code snippet and runs sequentially by the processor.
2. Read-only segment (RO data):
Read-only data segments are data that the program uses that are not changed, and are used in such a way as to look up a table, as these variables do not need to be changed, so they only need to be placed in read-only memory.
3. Read-write data segment (RW data) has been initialized:
Initialized data is declared in the program, and has the initial value of the variable, these variables need to occupy memory space, when the program executes, they need to be in a writable memory area, and have an initial value for the program to read and write.
4. Uninitialized read-write data segment (BSS):
Uninitialized read-write data is declared in the program, but there are no initialized variables that 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 assigned and freed by programmers. In the case of an operating system, if the programmer is not released, the operating system can reclaim memory at the end of the program.
6. Stacks (Stack):
Stack memory only appears when the program is running, variables used inside the function, parameters of functions and return values will use the stack space, and the stack space is automatically allocated and freed by the compiler.
Memory layout of the C language destination file
Code snippets, read-only data segments, read and write data segments, uninitialized data segments are static areas, and heaps and stacks belong to dynamic regions. Code snippets, read-only data segments and read-write data segments will be generated after the connection, and uninitialized data segments will be opened when the program is initialized, and the heap and stack will be allocated and disposed of in the run of the program.
The C language Program is divided into two states, image and run time, in the image formed after compiling the connection, will only contain code snippets, read-only data segments and read-write data segments, before the program runs, will dynamically generate uninitialized data segments, the program will also dynamically form the heap area and the stack area when running.
In general, in a static image file, each part is called a section, while the parts at run time are called segments (Segment), sometimes collectively referred to as segments.
II: Paragraph of the C language program
1. Code Snippet
The code snippet is generated by each function, and each statement of the function will eventually be compiled Yi and assembled to generate the binary machine code (the machine code for which architecture is specific is determined by the compiler).
2. Read-only segment (RO data)
Read-only data segments are generated by the data used in the program, which is characterized by the fact that it does not need to be changed in the run, so the compiler places the data segment into a read-only section. Read-only global variables in the C language, read-only local variables, constants used in programs, and so on are put into the read-only data area at compile time. Attention:
Defines the global variable const char a[100]={"ABCDEFG"}, generates a read-only data region of size 100 bytes, and initializes it with "ABCDEFG". If the definition is: const char a[]={"ABCDEFG"}, a 8-byte read-only data segment (and ' + ') is generated based on the length of the string, so a full initialization is generally required in the read-only data segment.
3. Read-write Data segment (RW)
The read-write data segment represents a portion of a data area that can be read or written in the target file, and in some cases they are also referred to as initialized data segments, which are part of the data segment and the code snippet that belongs to the static area of the program, but is writable in the same way as read-only data segments. Generally initialized global variables and local static variables are placed in the read-write data segment, such as: Define the static char b[100]={"ABCDEFG"} in the function, the read-write data area is characterized by the program must be initialized, if only defined, no initial value, will not generate read-write data area, Instead, it is positioned as an uninitialized data area (BSS).
If a global variable (a variable defined outside the function) is added to the static modifier, this means that it can only be used within a file, not by another file.
4. Uninitialized data segment (BSS)
Like a read-write data segment, it is also part of a static data area, but the data in that segment is not initialized. So it is only identified in the target file, not really a paragraph in the target file, which will be generated at run time. Uninitialized data segments are generated only 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 use of variables also have the following points to note:
1. Variables defined in the function body are usually on the stack and do not need to be managed in the program, and are handled by the Yi.
2. The memory space allocated by the function allocating memory, such as Malloc,calloc,realloc, is on the heap, and the program must be guaranteed to release with free, otherwise a memory leak will occur.
3. All functions are defined as global variables, and static variables are placed in the global zone either inside or outside the function.
4. Variables defined with const will be placed in the program's read-only data area.
Third: The use of the middle of the program
The following is a simple example to illustrate the corresponding relationship between variables and segments in C language. C Language Program in the global Zone (static area), the actual corresponding to the following paragraphs: RO Data; RW Data; BSS Data.
Generally speaking, a directly defined global variable is in the uninitialized data area, and if the variable has initialization it is in the initialized data area (RW data), plus the const is placed in the read-only data area.
Example: const char ro[] = {"This was read only data"}; //read-only data area
Static char rw_1[] ={"This is global read write data"}; Read-write Data segment initialized
Char bss_1[100]; Uninitialized data segment
const char *ptrconst = "constant data"; String in a read-only data segment
int main ()
{
Short B; On the stack, occupy 2 bytes
Char a[100]; Open 100 bytes on the stack, the value of the work is its first address
Char s[]= "ABCDEFG"; s on the stack, takes 4 bytes
The "ABCDEFG" itself is placed in a read-only data store, accounting for 8 bytes
Char *p1; P1 on the stack, taking up 4 bytes
Char *p2= "123456"; P2 on the stack, the contents of the P2 point can not be changed,
"123456" in the read-only data area
static char rw_2[]={"This is local read write data"};//locally initialized read-write segment
static Char bss_2[100]; Partially uninitialized data segment
static int c = 0; Global (static) initialization zone
p1= (char *) malloc (* sizeof (char)); Allocating memory areas in the heap area
strcpy (P1, "xxxx"); "XXXX" is placed in a read-only data area, accounting for 5 bytes
Free (p1); Use free to release the memory pointed to by P1
return 0;
}
The read-write data segment contains a global variable of the memory initialization, static char rw_1[], and a local static variable static rw_2[]. The difference is that when compiling Yi, it is used in the function or in the whole file. For rw_1[] Whether or not static modification, it will be placed in the read-write data area, but can be referenced by other files or not. For 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 completely changed, it will be open in the stack space of local variables, rather than static variables, here rw_1[],rw_2[] no specific values, Indicates that the static area size is the same as the subsequent string length.
For uninitialized data area bss_1[100] and bss_2[100], the difference is that the former is a global variable and can be used in all files, while the latter is a local variable that is used only inside the function. Uninitialized data segments do not set subsequent initialization values, so the size of the range must be specified using numeric values, and the Yi will set the length of the BSS to be increased by size.
The stack space is primarily used for storage of the following 3 data:
1. Dynamic variables inside the function
2. Parameters of the function
3. return value of function
The stack space is dynamically opened and recycled. In the process of function call, if the function call hierarchy is more, the required stack space is also gradually increased, for the parameters of the transfer and return value, if the use of large structure, the use of the stack space will be relatively large.
Http://www.cnblogs.com/chenyadong/archive/2011/11/28/2266506.html
Memory layout for C language programs