The difference between static area, stack and heap in linux memory

Static area : save automatic global variables and static variables (including static global and local variables). The content of the static area exists in the life cycle of the whole program, and is allocated by the compiler when compiling.

stack is a stack : save local variables, and its space is automatically allocated and released by the operating system. The content on the stack only exists within the scope of the function, and the parameters are passed on the stack when the function is called during the running of the program. When the function finishes running, the content will be automatically destroyed. It is characterized by high efficiency, but limited space.

  • Of course, the stack also has dynamic allocation, which is allocated by the alloca function, but the dynamic allocation of the stack is different from the heap. Its dynamic allocation is released by the compiler without any manual implementation. It is worth noting that although the alloca function can be used to realize the dynamic allocation of stack memory, the portability of the alloca function is very poor, and it is difficult to implement on a machine without a traditional stack. Therefore, it should not be used in widely ported programs. Of course, you can use the variable-length array in C99 to replace the alloca function.

Heap is a heap : its space is manually allocated/released, such as the memory allocated by the malloc series of function operators. Its life cycle is determined by free etc. Exists until the end of the program until it is released. It is characterized by flexible use and relatively large space, but it is easy to make mistakes.

For heap and stack:

1) Allocation fragmentation problem

For the heap, frequent allocation and release (malloc / free) of different sizes of heap space will inevitably cause discontinuity in the memory space, resulting in a large number of fragments, resulting in reduced program efficiency; but for the stack, this problem does not exist .

2) Efficiency of distribution

As we all know, the stack is a data structure provided by the machine system. The computer will provide support for the stack at the bottom layer. For example, a special register is allocated to store the address of the stack. The efficiency is relatively high. Generally speaking, as long as the remaining space of the stack is larger than the requested space, the system will provide memory for the program; otherwise, an exception will be reported indicating stack overflow.

The heap is different, it is provided by the C/C++ function library, and its mechanism is quite complicated. For example, in order to allocate a piece of heap memory, you should first know that the operating system has a linked list that records free memory addresses. When the system receives an application from a program, it will traverse the linked list to find the first heap node whose space is larger than the requested space, and then Delete the node from the free node list, and allocate the space of the node to the program. For most systems, the size of this allocation will be recorded at the first address of this memory space, so that the delete statement in the code can correctly release this memory space. In addition, since the size of the found heap node is not necessarily exactly equal to the requested size, the system will automatically put the redundant part back into the free list. Obviously, the allocation efficiency of the heap is much lower than that of the stack.

3) Application size limit

Because the operating system uses a linked list to store free memory addresses (the memory area is not continuous), and the traversal direction of the linked list is from low address to high address. Therefore, the application size of the heap memory is limited by the available virtual memory in the computer system.

The stack is different. It is a continuous memory area, and the growth direction of its address is downward, and it grows in the direction of decreasing memory address. It can be seen that the address of the top of the stack and the maximum capacity of the stack are generally predetermined by the system. If the requested space exceeds the remaining space of the stack, an overflow error will be prompted. It can be seen that, compared with the heap, the space that can be obtained from the stack is relatively small.

4) Stored content

For the stack, it is generally used to store function parameters and local variables. For example, when a function is called, the first thing that is pushed into the stack is (in the main function) the address of the next instruction at the call (that is, the next executable statement of the function call statement), and then the parameters of the function. In most C compilers, parameters are pushed onto the stack from right to left, and finally local variables in the function (note that static variables are not pushed onto the stack)

Guess you like

Origin blog.csdn.net/weixin_42432281/article/details/85268625