Differences between data segments, code segments, stack segments, and BSS segments

Source: Internet
Author: User
Five data segment processes (executed programs) occupy a certain amount of memory. They are used to store program code loaded from the disk or user input data. However, the Process manages these memories differently because of different memory usage. Some memories are allocated statically and centrally in advance, while others are allocated and recycled dynamically as needed. For any common process, it involves five different data segments.

Five Linux Process segments

Next we will briefly summarize what the five different data zones in the memory space corresponding to the process are doing.

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.

It is allocated by the operating system, and the application and recovery of memory are all managed by the OS.

PS:

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.

The BSS segment (data not manually initialized) does not allocate space for the data segment, but records the size of the space required for the data.
Data (data that has been manually initialized) segments allocate space for the data and store the data in the target file. The data segment contains the initialized global variables and their values. The size of the BSS segment is obtained from the executable file, and then the linker obtains the size of the memory block, followed by the data segment. When this memory area enters the address space of the program, all are cleared. The entire section that contains data segments and BSS segments is usually called a data zone.

Five data segment processes (executed programs) occupy a certain amount of memory. They are used to store program code loaded from the disk or user input data. However, the Process manages these memories differently because of different memory usage. Some memories are allocated statically and centrally in advance, while others are allocated and recycled dynamically as needed. For any common process, it involves five different data segments.

Five Linux Process segments

Next we will briefly summarize what the five different data zones in the memory space corresponding to the process are doing.

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.

It is allocated by the operating system, and the application and recovery of memory are all managed by the OS.

PS:

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.

The BSS segment (data not manually initialized) does not allocate space for the data segment, but records the size of the space required for the data.
Data (data that has been manually initialized) segments allocate space for the data and store the data in the target file. The data segment contains the initialized global variables and their values. The size of the BSS segment is obtained from the executable file, and then the linker obtains the size of the memory block, followed by the data segment. When this memory area enters the address space of the program, all are cleared. The entire section that contains data segments and BSS segments is usually called a data zone.

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.