Differences between BSS and data segments

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.