------------------------------------------------------------------------------------------------------
The main differences between heap and stack are the following points:
------------------------------------------------------------------------------------------------------
1) Management methods and fragmentation issues
For the stack, is automatically managed by the compiler, without manual control, for the heap, the release of work by the programmer to control, prone to memory fragmentation, for the heap, frequent new/delete will inevitably cause memory space discontinuity, resulting in a large number of fragments, so that the efficiency of the program is reduced. For the stack, there is no such a problem, because the stack is advanced out of the queue, it is not possible to have a non-stack of memory block from the middle of the stack pop-up, before it pops up, its upper-back stack content has been ejected, so there is no discontinuous fragmentation.
------------------------------------------------------------------------------------------------------
2) Allocation efficiency
Stack is the data structure provided by the system, the computer will support the stack at the bottom, allocate the address of the special register storage stack, and the stack stack has special instruction execution, which determines the efficiency of the stack is higher. The heap is provided by C + +, and its mechanism is relatively complex; for example, allocating a piece of memory will search the heap memory for a sufficient amount of space available, if there is not enough memory space (possibly due to too much memory fragmentation), according to a certain algorithm (memory allocation algorithm). It is possible to invoke the system function to increase the memory space of the program data segment, so that there is a chance to divide the memory in sufficient size and then return. Obviously, the heap is much less efficient than the stack.
------------------------------------------------------------------------------------------------------
3) The direction of growth is different
stack memory by a stack pointer esp to open up and recycle, stack memory from high address to low address growth, growth, the stack pointer is moving to the low address direction, the pointer address value is correspondingly reduced; when recycling, the stack pointer moves to the high address direction, the address value increases. Therefore, the stack memory of the opening and recycling are only pointers to the addition and subtraction.
For the heap, the direction of growth is upward, that is, in the direction of the memory high address, when recycling, the pointer moves to the low address direction, the address value is reduced.
------------------------------------------------------------------------------------------------------
4) Different space sizes
Generally speaking 32-bit system, heap memory can reach 4GB space, from this point of view heap memory is almost no limit. But for the stack, generally there is a certain amount of space. Whether it is a heap or stack, it is to prevent the occurrence of cross-border phenomenon, because the result of cross-border is either a program crash, or destroy the program heap, stack structure, produce unexpected results.
------------------------------------------------------------------------------------------------------
This article is from the "Nobody" blog, please be sure to keep this source http://814193594.blog.51cto.com/10729329/1732670
"Programmers have to know" the main difference between heaps and stacks