Differences in data segments under Linux (data segment, code snippet, stack segment, BSS segment)

Source: Internet
Author: User

A process (executing a program) consumes a certain amount of memory, either to store program code loaded from disk, or to store data from user input, and so on. However, the way the process manages these memory is different from memory usage, some of which are statically allocated and collected uniformly, while others are dynamically allocated and reclaimed as needed. For any ordinary process, it involves 5 different pieces of data.

Five segments of a Linux process

Let's briefly summarize what 5 different data areas are included in the memory space of the process.

BSS segment: A BSS segment (BSS segment) usually refers to an area 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 usually refers to an area of memory that is used to hold the initialized global variables in the program. The data segment belongs to static memory allocation .

Code Snippets: code segment/text segment usually refers to an area of memory 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, and some schemas allow the code snippet 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 used to store a dynamically allocated memory segment in a process run, which 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), and freed memory is removed from the heap when memory is freed (heap is scaled down).

Stack: Stacks are also called stacks, which are local variables that are temporarily created by the user's program, that is, the variables defined in the parentheses "{}" (but not the variables that are static declarations, static means that the variables are stored 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. Due to the advanced first-out features of the stack, the stack is particularly handy for saving/recovering call sites. In this sense, we can think of the stack as a memory area where temporary data is stored and exchanged.

It is assigned by the operating system, and memory requests and recoveries are managed by the OS.

Ps:

The global uninitialized variable exists in the. BSS segment, which 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 does not occupy the. exe file space, its contents are initialized by the operating system (0), while. Data needs to be occupied and its contents are initialized by the program, resulting in this situation.

A BSS segment (data that is not manually initialized) does not allocate space to the data for that segment, but only the amount of space required to record the data.
Data (data that has been manually initialized) segments are allocated space and the data is saved in the destination file. The 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 the data segment and the BSS segment is often referred to as the data area.

(RPM) The difference between data segments (data segments, code snippets, stack segments, BSS segments) under Linux

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.