Definition and difference of stack

In our daily work, we often talk about the stack. Although stacks are always mentioned together, they are actually two different concepts.

1. Simple understanding of the stack

First of all, to put it simply, both the heap and the stack are in RAM. Generally, the allocated area is the same area, but the heap is stored from bottom to top, and the stack is stored from top to bottom. At this time, it should be noted that the heap and stack may step on each other's memory during the process of continuous growth, and unexpected errors will occur. Therefore, when using it, you should pay attention to releasing the stack in time or dividing the memory larger. In addition, the stack is automatically allocated and released by the compiler, such as function parameters, local variable values, etc. The heap is generally allocated and released by the programmer, and the allocation method is similar to the linked list in the data structure.

2. Definition of stack

The following examples illustrate in detail:

 As shown in the sample program above, global variables and constants belong to the static area (Static), which is allocated in advance by the compiler, and the life cycle runs through the entire program; the parameter value of the function and the value of the local variable belong to the stack (Stack), which is determined by the compiler Automatic allocation and release; programmers use the malloc function to dynamically request the allocated memory space to belong to the heap (Heap). It is worth noting that if you forget to use the free function to release the memory after the dynamically allocated memory is used up, it will cause a memory leak, and when the heap and stack grow endlessly to cover each other, many unpredictable problems will occur . The program may run away and fly away.

2.1 static

The three segments .bss .data .text should belong to the static segment.

BSS segment (bss segment): Usually refers to a memory area used to store uninitialized global variables in the program. The BSS segment is a static memory allocation.

Data segment: Usually refers to a memory area used to store initialized global variables in the program, and also belongs to static memory allocation.

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 set to read-only permissions. Of course, in In some architectures, the code segment is also allowed to be writable, that is, the program is allowed to be modified. In the code segment, some read-only constant variables may also be included, such as string constants.

2.2 Heap

Heap (heap): Used to store the memory segment that is dynamically allocated during the running of the process. The size is not fixed and can be dynamically expanded or reduced. When the process calls functions such as malloc to allocate, the newly allocated memory is dynamically added to the heap , when functions such as free are called to free memory, the freed memory will be removed from the heap (the heap will be shrunk).

2.3 stack

Stack (stack): Also known as the stack, it is a local variable temporarily created by the user to save the program. When the function is called, its parameters (essentially registers, eax, ebx) will be pushed into the stack. After the calling function is executed , the parameters pushed onto the stack will be popped out. In addition, the temporary variables defined in the function body will be stored in the stack. The stack is allocated by the operating system, and the application and recovery of memory are determined by the os.

Note: Static variables, also known as static variables, are stored in the data segment. Regardless of whether the variable is a global variable or a local variable, the scope of static global variables is limited to the file, while the scope of static local variables is only within the compound statement in which it is defined. As long as the variable declared static will be stored in the data segment, if not assigned, it will default to 0.

 

Guess you like

Origin blog.csdn.net/panpan_jiang1/article/details/127820164