A program is essentially composed of BSS, data, and text segments three. This concept is very important in the current computer programming, and is very important in the design of the embedded system, which involves the memory size allocation of the embedded system and the storage unit occupying space.
- BSS segment: In an architecture that uses segment memory management, the BSS segment (BSS segment) usually refers to an area of memory that is used to hold uninitialized global variables in the program. BSS is the abbreviation for English block Started by symbol. BSS segments belong to static memory allocations.
- Data segment: In a segment-based memory management architecture, data segment usually refers to a piece of memory that is used to hold the initialized global variables in the program. The data segment belongs to static memory allocation.
- Code snippets: In a schema with segment memory management, the code snippet (text segment) usually refers to a piece of memory area that is used to hold the program execution code. The size of this part of the area is determined before the program runs and the memory area is read-only. In a code snippet, it is also possible to include some read-only constant variables, such as String constants.
The target file generated after the program compiles contains at least these three segments, and the approximate structure of the three segments is as follows:
The . Text is the code snippet, which is read-only: The BSS segment contains uninitialized global variables and static variables in the program. The data segment contains three parts: heap, Stack (stack), and static data area.
- Heap: A heap is used to store a dynamically allocated memory segment in a process run, which is not fixed in size and can be dynamically expanded or scaled down. When a process calls a function such as malloc to allocate memory, the newly allocated memory is dynamically added to the heap (heap is expanded), and freed memory is removed from the heap when memory is freed (heap is scaled down).
- Stack: Stacks are also called stacks, which are local variables that are temporarily created by the user's program, that is, the variables defined in the parentheses "{}" (but not the variables that are static declarations, static means that the variables are stored in the data segment). In addition, when a function is called, its arguments are also pressed into the process stack that initiates the call, and the return value of the function is stored back to the stack when the call ends. Due to the advanced first-out features of the stack, the stack is particularly handy for saving/recovering call sites. In this sense, we can think of the stack as a memory area where temporary data is stored and exchanged.
When a program dynamically allocates space (the malloc function in C) at execution time, the allocated space belongs to the heap. Its concept differs from the concept of "heap" in data structures.
The stack segment holds variables, parameters, and return addresses inside the function, which are automatically assigned when the function is called, and access is the LIFO method in the standard stack. (because the local variables of the function are stored here, it should be accessed in a way that the stack pointer is offset, otherwise it is cumbersome to access through push, pop operations)
The static data section in the dataset holds the initialized global variables, static variables, and constants in the program.
In an architecture that uses segment memory management (such as Intel's 80x86 system), the BSS segment (Block Started by Symbol segment) usually refers to a chunk of memory that is used to hold uninitialized global variables in the program, and generally the BSS segment will be zeroed when initialized. The BSS segment is a static memory allocation, where the program is zeroed out at the outset.
For example, after a program such as C has been compiled, the initialized global variable is saved in the. Data segment, and the uninitialized global variable is saved in the. BSS segment.
Both the text and data fields are in the executable file (typically cured in an embedded system in an image file) and are loaded from the executable, while the BSS segment is not in the executable file and is initialized by the system.
Figure from "c expert programming"
The BSS segment only holds variables that have no value, so in fact it does not need to save the images of those variables. The size of the BSS segment required for the runtime is recorded in the destination file, but the BSS segment does not occupy any space in the destination file.
- Main.c
- int a = 0; //Global initialization Zone
- Char *p1; //Global uninitialized zone
- Main ()
- {
- static int c =0; //Global (Static) initialization area
- int b; //Stack
- char s[] = "abc"; //Stack
- Char *p2; //Stack
- char *p3 = "123456"; //"123456\0" in the constant area, p3 on the stack.
- P1 = (char *) malloc (10);
- P2 = (char *) malloc (20); //The area allocated 10 and 20 bytes is in the heap area.
- }
C Program code Memory layout (Learn the basics of C)