BSS segment, data segment, text segment, heap (heap), and stack (stack)

Source: Internet
Author: User

BSS segment:

BSS segment (BSS segment)   usually refers to a chunk of memory that is used to hold uninitialized global variables in the program.

BSS is the abbreviation for English block Started by symbol.

BSS segments belong to static memory allocations.


Data segment:

data Segment (segment)   usually refers to a chunk of memory that is used to hold the initialized global variables in the program.

The data segment belongs to static memory allocation.


Text paragraph:

Snippet (code segment/text segment)   usually refers to a piece of memory area that is used to store program execution code.

The size of this area is determined before the program runs, and the memory area is usually read-only (some schemas also allow code snippets to be writable, which allows the program to be modified).

In a code snippet, it is also possible to include some read-only constant variables, such as String constants.


Heap:

  A heap is a memory segment that is allocated dynamically during a process run and is not fixed in size and can be dynamically expanded or scaled down.

When a process calls a function such as malloc to allocate memory, the newly allocated memory is dynamically added to the heap (heap is expanded);

When the memory is freed using functions such as free, the freed memory is removed from the heap (the heap is scaled down).


Stacks (Stack)

Stacks, also known as stacks, are temporary local variables created by the user store program,

That is, we function the variables defined in brackets "{}" (but do not include variables of static declarations, static means storing variables in the data segment).

In addition, when a function is called, its arguments are also pressed into the process stack that initiates the call, and the return value of the function is stored back to the stack when the call ends.

The stack is particularly handy for saving/recovering call sites due to the advanced first-out (FIFO) features of the stack.

In this sense, we can think of the stack as a memory area where temporary data is stored and exchanged.

A program is essentially composed of BSS, data, and text segments three.

  Such a concept, do not know where the initial origin of the provisions, but in the current computer programming is very important a 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), BSS usually refers to an area of memory that is used to hold uninitialized global variables in the program.

In general, the BSS section will be zeroed out at initialization time. The BSS segment is a static memory allocation, where the program is zeroed out at the outset.

For example, after a program such as C has been compiled, the initialized global variable is saved in the. Data segment, and the uninitialized global variable is saved in the. BSS segment.

  Both the text and data fields are 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.

CasesTwo small programs are as follows:

Program 1:
int ar[30000]; void Main () {    ...}


Program 2:

int ar[300000] = {123456  }; void Main () {    ...}
the. exe file found after Program 2 compilation is much larger than program 1. The present is very puzzled, so I compiled it by hand, and used the /FAS compilation option to view its respective . ASM, found in program 1.asm AR is defined as follows: _BSS SEGMENT
[Email Protected]@3paha DD 0493e0h DUP (?) ; Ar
_BSS ENDS
In program 2.asm,ar is defined as: _data SEGMENT
[Email Protected]@3paha DD 01H; Ar
DD 02H
DD 03H
ORG $+1199988
_data ENDS
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; and the automatic variables within the function allocate space on the stack;. BSS is not occupied with the. exe file space, and its contents are initialized by the operating system (0); . Data needs to be occupied, and its contents are initialized by the program. as a result, this is the case. The BSS segment (data not initialized manually) does not allocate space for the data, only the size of the space required to record the data;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. The data segment (data that has been manually initialized) is the space for which the data is allocated and stored in the target file;The data segment contains the initialized global variables and their values. when this memory area enters the program's address space, it is all zeroed out. the entire segment, which contains the data and BSS segments, is often referred to as an area.

BSS segment, data segment, text segment, heap (heap), and stack (stack)

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.