Difference between BSS segment and data segment

Source: Internet
Author: User

In general, a program is essentially composed of BSS,data,text three-the concept of the current computer programming is a very important basic concept. It is also very important in the design of embedded system, which involves the allocation of memory size and the size of storage unit in the runtime of embedded system.

In an architecture that uses segment memory management (such as Intel's 80x86 system), theBSS segment (Block Started by Symbol segment) usually refers to an area of memory that is used to hold uninitialized global variables in the program, In general, the BSS segment will be cleared at initialization 0 (BSS is astatic memory allocation, i.e. the program is zeroed out at the beginning).

For example, after the C language program has been compiled, the initialized global variables are saved in the . Data segment, and the uninitialized global variables are saved in the . BSS segment.    

The text and data fields are both in the executable file (usually cured in the embedded system in the image file), and the system is loaded from the executable file;

The BSS segment is not in the executable file and is initialized by the system.

Compile two small programs as follows:

Program 1:

int ar[30000];
void Main ()
{

......

}

Program 2:

int ar[300000] = {1, 2, 3, 4, 5, 6};
void Main ()
{

......

}

the. exe file found after Program 2 compilation is much larger than program 1. Why?

The difference is obvious, one is located in the . BSS segment, and the other is in . Data, the difference is:

The global uninitialized variable exists in the . BSS segment, and is represented by a placeholder; the global initialized variable is stored in the . Data segment;

L and the automatic variables inside the function allocate space on the stack.

L . BSS is not occupied with the. exe file space, and its contents are initialized by the operating system (0);

While . Data needs to be occupied and its contents are initialized by the program, this is the case.

Attention:

The BSS segment (data that has not been manually initialized) does not allocate space for the segment's data, but only the amount of space required to record the data.

The data (data that has been manually initialized) segment is allocated to the space, and the data is saved in the target file.

The l data segment contains the initialized global variables and their values.

the size of the BSS section is obtained from the executable file, and then the linker gets a block of memory of that size, immediately following the data segment. When this memory area enters the program's address space, it is all zeroed out. The entire segment that contains data and BSS segments is often referred to as the data area.

Difference between BSS segment and data segment

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.