Category: Embedded driver development
Use small code to experience the difference between BSS segments and data in the program
| BSS (block started by symbol) is used to store uninitialized global and static variables. The values of global or static variables are 0 or null (for pointer variables) is considered by the compiler to be uninitialized and belongs to the static memory allocation area, which does not occupy the space of program files and does not store these variables, but still occupies part of the space, these spaces are used to identify uninitialized variable sizes, attributes, and other information.
Data
It is used to store initialized global variables and also belongs to the static memory allocation area, which occupies the space of program files.
Text
Code snippet, which stores Code commands executed by the program and occupies the space of the program file
Code 1:
Int A [1024*1024*10] = {0}
Int main () {return 0 ;}
Code 2:
Int A [1024*1024*10] = {1}
Int main () {return 0 ;}
With the same compilation of the previous two sections of code, we can see that the file size is quite different. Code 2 calculates array. |
Global uninitialized variables exist in. the BSS segment is embodied as a placeholder; the Global initialized variables are stored in. data segment; and the automatic variables in the function are allocated space on the stack .. BSS does not occupy the. exe file space, and its content is initialized (cleared) by the operating system, while. data needs to be occupied, and its content is initialized by the program. That is to say, the BSS segment (data not manually initialized) does not allocate space for the segment, but only records the size of the space required for the data.
Data (data that has been manually initialized) segments allocate space for the data and store the data in the target file.
The data segment contains the initialized global variables and their values.
The size of the BSS segment is obtained from the executable file, and then the linker obtains the size of the memory block, followed by the data segment. When this memory area enters the address space of the program, all are cleared. The entire section that contains data segments and BSS segments is usually called a data zone.