BSS segment, data segment, code segment, heap and stack.

Source: Internet
Author: User

 

BSS segment: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:A data segment is a memory area used to store initialized global variables in a program. The data segment belongs to the static memory allocation.

Code segment:A code segment or text segment is a memory area used to store code execution. The size of this area is determined before the program runs, and the memory area is usually read-only,
Some architectures also allow code segments to be writable, that is, programs can be modified. In the code segment, it may also contain some read-only constant variables, such as string constants.

Heap ):Heap is used to store the memory segments dynamically allocated during the process running. 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)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 ARC "{}" of our function (but does not include static declared variables, static means to store variables 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.

[Example 1]

Compile two small programs with Cl as follows:

Procedure 1:

Int ar [30000];
Void main ()
{
......
}


Procedure 2:

Int ar [300000] =
{1, 2, 3, 4, 5, 6 };
Void main ()
{
......
}


Find that the. EXE file after compilation of Program 2 is much larger than that of program 1. Now I am puzzled, So I compiled it manually and used the/Fas compilation option to check their respective. ASM. I found that the AR definition in program 1. ASM is as follows:

_ BSS segment
? Ar @ 3 Paha dd 0493e0h DUP (?) ; Ar
_ BSS ends


In program 2. ASM, Ar is defined:

_ Data Segment
? Ar @ 3 Paha dd 01 H; ar
Dd 02 h
Dd 03 h
Org $ more 1199988
_ DATA ends


The difference is obvious. BSS segment, while the other is located in. data segment, the difference between the two is: 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. The content is initialized (cleared) by the operating system, but. Data is occupied. The content is initialized by the program, which leads to the above situation.

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.