The main difference between heap and stack is as follows:

1. Different management methods; 2. Different space
sizes;
3. Different fragmentation;
4. Different growth directions;
5. Different allocation methods;
6. Different allocation efficiency ; Automatic management, without our manual control; for the heap, the release work is controlled by the programmer, which is prone to memory leaks. Space size : Generally speaking, under a 32-bit system, the heap memory can reach 4G of space. From this point of view, there is almost no limit to the heap memory. But for the stack, there is generally a certain space size. For example, under VC6, the default stack space size is 1M. Of course, this value can be modified. Fragmentation problem : For the heap, frequent new/delete will inevitably cause discontinuity in the memory space, resulting in a large number of fragments and reducing the efficiency of the program. For the stack, this problem does not exist, because the stack is a first-in-last-out queue, and they are so one-to-one that it is never possible to have a memory block popped from the middle of the stack. The content of the backward stack above him has been popped up. For details, please refer to the data structure. Growth direction : For the heap, the growth direction is upward, that is, in the direction of increasing memory addresses; for the stack, its growth direction is downward, which is in the direction of decreasing memory addresses. Allocation method : The heap is dynamically allocated, and there is no statically allocated heap. The stack has two allocation methods: static allocation and dynamic allocation. Static allocation is done by the compiler, such as the allocation of local variables. The dynamic allocation is allocated by the malloca function, but the dynamic allocation of the stack is different from that of the heap. His dynamic allocation is released by the compiler, and we do not need to implement it manually. allocative efficiency





: The stack is a data structure provided by the machine system. The computer will provide support for the stack at the bottom layer: allocating a special register to store the address of the stack, and pressing and popping out of the stack have special instructions to execute, which determines the high efficiency of the stack. The heap is provided by the C/C++ function library, and its mechanism is very complicated. For example, in order to allocate a piece of memory, the library function will search the heap memory according to a certain algorithm (for the specific algorithm, please refer to the data structure/operating system). If there is not enough space (probably due to too many memory fragments), it is possible to call the system function to increase the memory space of the program data segment, so that there is a chance to allocate enough memory, and then proceed to return. Obviously, the heap is much less efficient than the stack.
From here, we can see that compared with the stack, due to the use of a large number of new/delete, it is easy to cause a lot of memory fragmentation; because there is no special system support, the efficiency is very low; due to the possibility of switching between user mode and kernel mode, The application of memory becomes more expensive. Therefore, the stack is the most widely used in the program. Even the function call is completed by the stack. The parameters, return address, EBP and local variables in the function call process are stored in the stack. Therefore, we recommend that you try to use the stack instead of the heap.
Although the stack has so many benefits, because it is not so flexible compared to the heap, sometimes it is better to use the heap to allocate a large amount of memory space.
Whether it is the heap or the stack, it is necessary to prevent the occurrence of out of bounds (unless you deliberately make it out of bounds), because the result of crossing the bounds is either the program crash, or the destruction of the program's heap and stack structures, resulting in unexpected results.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324604564&siteId=291194637