A program is essentially composed of three parts: BSS segment, data segment, and text segment. This concept is a very important basic concept in the current computer program design. It is also very important in the design of embedded systems, involving the memory size allocation during running of embedded systems, storage Unit space usage problems.
- BSS segments: In a segmented memory management architecture, BSS segments (bss segments) are generally a memory area used to store uninitialized global variables in the program. BSS is short for Block Started by Symbol. BSS segments belong to static memory allocation.
- Data segment: In a segmented memory management architecture, data segments (data segments) are generally a memory area used to store initialized global variables in the program. The data segment belongs to the static memory allocation.
- Code segment: In a segmented memory management architecture, a text segment is usually a memory area used to store code execution. The size of this area is determined before the program runs, and the memory area is read-only. In the code segment, it may also contain some read-only constant variables, such as string constants.
The target file generated after the program compilation contains at least these three segments. The structure of these three segments is as follows:
The. text is the code segment and is read-only .. The bss segment contains uninitialized global and static variables in the program. The data segment consists of three parts: heap, stack, and static data zone.
- Heap: a heap is used to store the memory segments dynamically allocated during a process. Its size is not fixed and can be dynamically expanded or reduced. When a process calls a function such as malloc to allocate memory, the newly allocated memory is dynamically added to the heap (the heap is expanded). When a function such as free is used to release the memory, released memory is removed from the heap (the heap is reduced)
- Stack: A stack, also known as a stack, is a local variable temporarily created by the user to store the program. That is to say, the variable defined in the callback arc "{}" (but does not include static declared variables, static means storing changes in data segments ). In addition, when a function is called, its parameters will also be pushed into the process stack that initiates the call. After the call is completed, the return values of the function will also be stored in the stack. Because of the stack's first-in-first-out feature, the stack is particularly convenient for storing/restoring the call site. In this sense, we can regard the stack as a memory zone for storing and exchanging temporary data.
When the program dynamically allocates space during execution (the malloc function in C), the allocated space belongs to heap. The concept is different from the concept of "heap" in the data structure.
The stack segment stores the variables, parameters, and return addresses in the function. It is automatically allocated when the function is called. The access method is the LIFO method in the standard stack. (Because the local variables of the function are stored here, the access method should be the stack pointer plus offset method. Otherwise, it is quite troublesome to access the function through the push and pop operations)
The static data area in the data Segment stores the initialized global variables, static variables, and constants in the program.
In an architecture that adopts segmented memory management (such as intel's 80x86 system) and BSS segment (Block Started by Symbol segment) it is usually a memory area used to store uninitialized global variables in the program. Generally, the BSS segment is cleared during initialization. The BSS segment is a static memory allocation, that is, the program clears it from the beginning.
For example, after a program such as C language is compiled, initialized global variables are saved in the. data Segment, and uninitialized global variables are saved in the. bss segment.
Both the text and data segments are in the executable file (which is usually fixed in the image file in the embedded system) and loaded by the system from the executable file; the BSS segment is not in the executable file and is initialized by the system.
The BSS segment only saves variables without values, so in fact it does not need to save the images of these variables. The BSS segment size required for running is recorded in the target file, but the BSS segment does not occupy any space in the target file.
- // Main. c
- Int a = 0; // global initialization Zone
- Char * p1; // not initialized globally
- Main ()
- {
- Static int c = 0; // global (static) initialization Zone
- Int B; // Stack
- Char s [] = "abc"; // Stack
- Char * p2; // Stack
- Char * p3 = "123456"; // "123456 \ 0" is in the constant area, and p3 is in the stack.
- P1 = (char *) malloc (10 );
- P2 = (char *) malloc (20); // The allocated 10-byte and 20-byte areas are in the heap area.
- }