five data segments

The 5 data segment
       processes (executed programs) will occupy a certain amount of memory, which is either used to store program code loaded from disk, or to store data input from the user, and so on. However, the way the process manages these memories varies due to different uses of the memory. Some memory is statically allocated and reclaimed uniformly in advance, while some is dynamically allocated and reclaimed as needed. For any ordinary process, it will involve 5 different data segments.
The five segments of a Linux process:

BSS segment: The bss segment belongs to static memory allocation. bss is the abbreviation of English Block Started by Symbol.
 The BSS segment usually refers to a memory area used to store uninitialized global variables and static variables in the program. The feature is readable and writable, and the BSS segment will be automatically cleared to 0 before the program is executed, so the uninitialized global variables have become 0 before the program is executed.

Data segment: The data segment usually refers to a memory area used to store initialized global variables in the program. The data segment is a static memory allocation.

Code segment: A code segment (code segment/text segment) usually refers to a memory area used to store program execution code. The size of this part of the area has been determined before the program runs, and the memory area is usually read-only, and some architectures also allow the code segment to be writable, which allows the program to be modified. In the code segment, it is also possible to contain some read-only constant variables, such as string constants, etc.

Heap (heap): The heap is used to store the memory segment that is dynamically allocated during the running of the process. Its size is not fixed and can be dynamically expanded or reduced. When the process calls functions such as malloc to allocate memory, the newly allocated memory is dynamically added to the heap (the heap is expanded); when the memory is released by functions such as free, the released memory is removed from the heap (the heap is reduced)

Stack (stack): The stack, also known as the stack, is a local variable temporarily created by the user to store the program, that is to say, the variable defined in our function brackets "{}" (but not including the static declared variable, static means in the data segment storage variable). In addition, when the function is called, its parameters will also be pushed into the calling process stack, and after the call ends, the return value of the function will also be stored back on the stack. Due to the first-in-last-out feature of the stack, the stack is particularly convenient for saving/restoring the calling scene. In this sense, we can regard the stack as a memory area for storing and exchanging temporary data.

Guess you like

Origin blog.csdn.net/m0_57128077/article/details/128235210