"C language Common sense" mdk various section analysis __c language

Source: Internet
Author: User

Source: http://www.cnblogs.com/yanghong-hnu/p/4705755.html


BSS segment:

The BSS segment (BSS segment) is usually a memory area used to store uninitialized global variables in a program.

BSS is the abbreviation for English block started by symbol.

The BSS segment belongs to a static memory allocation.
Data section:

A data segment (segment) is typically an area of memory used to hold an initialized global variable in a program.

The data segment belongs to a static memory allocation.
Text paragraph:

Code Snippets (Segment/text segment) typically refer to a single area of memory used to hold program execution code.

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

In a code snippet, it is possible to include some read-only constant variables, such as String constants.
Heap (heap):

The heap is used to store the dynamically allocated memory segments in the process, and it is not fixed in size and can be dynamically expanded or reduced.

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

When freeing memory with functions such as free, freed memory is removed from the heap (the heap is reduced).
Stacks (Stack):

The stack is also called the stack, which is a local variable created by the user to store the program temporarily.

That is, the variables defined in the parentheses "{}" of our function (but not the variables of the static declaration, static means that the variables are stored in the data segment).

In addition, when a function is invoked, its arguments are pressed into the process stack that initiated the call, and the return value of the function is stored back into the stack when the call is finished.

Because of the advanced first Out (FIFO) features of the stack, the stack is particularly handy for saving/resuming call scenes.

In this sense, we can think of the stack as a memory area that registers and swaps temporary data.

A program is essentially composed of BSS section, data section, text paragraph three.

Such a concept, do not know where originally from the provisions, but in the current computer programming is a very important basic concept.

And in the design of embedded system is also very important, involving the embedded system running memory size allocation, storage units occupy space size problem.

In a segment-memory-managed architecture (such as Intel's 80x86 system), the BSS segment usually refers to an area of memory that holds uninitialized global variables in the program.

Generally, the BSS section will be zeroed when initializing. The BSS segment is a static memory allocation, which means that the program will clear zero at the beginning.

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

The text and data sections are in the executable file (in the embedded system is generally solidified in the mirror file), by the system loaded from the executable file;

The BSS section is not in the executable file and is initialized by the system. "Example" two small procedures are as follows:

Program 1:

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


Program 2:

int ar[300000] = {1, 2, 3, 4, 5, 6};
void Main ()
{
    ...
}
Discovery Program 2 after compiling the. exe file is much larger than the program 1. The moment was puzzled, so the manual compilation, and using the/FAS compilation option to view their respective. asm, found in the program 1.asm AR is defined as follows: _BSS SEGMENT
? Ar@@3paha DD 0493e0h DUP (?) ; Ar
_BSS ENDS
In program 2.asm, AR is defined as: _data SEGMENT
? Ar@@3paha DD 01H; Ar
DD 02H
DD 03H
ORG $+1199988
_data ENDS
The difference is obvious, one is located in the. BSS segment while the other is located. Data segment, the difference is that the global uninitialized variable exists in the. BSS segment, which is embodied as a placeholder; the global initialized variables are stored in the. Data segment, and the automatic variables within the function allocate space on the stack;. The BSS is not occupied with the. exe file space, and its contents are initialized by the operating system (clear 0);. Data needs to be consumed and its content is initialized by the program. As a result, this is the case. The BSS segment (data not manually initialized) does not allocate space for the data in the segment, but only the amount of space required to record the data; the size of the BSS is obtained from the executable file, and then the linker gets the size of the memory block, immediately after the data segment. The data segment (the data that has been manually initialized) is the space in which the data is allocated, which is saved in the destination file, and the database contains the initialized global variables and their values.   All zeros are cleared when this memory area enters the program's address space. The entire section that contains the data and BSS segments is now commonly referred to as an area.


References: http://blog.csdn.net/ouyang_linux007/article/details/7448814 http://www.cnblogs.com/mylinux/p/4135088.html

Related Article

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.