Management method:
for the stack, it is automatically managed by the compiler, without our manual control; for the heap, the release work is controlled by the programmer and easily generatedMemory Leak.
Application Size:
stack: inWindowsunder,A stack is a data structure that extends to a low address and is a contiguous area of memory. The top of the stack's address and the maximum capacity of the stack is the system pre-defined, inWINDOWSThe size of the stack is2M(and some say it's1M, which is a constant at compile time), if the requested space exceeds the remaining space on the stack, it will promptOverflow. Therefore, the space available from the stack is small.
Heap: A heap is a data structure that extends to a high address, and is a discontinuous area of memory. This is because the system is stored with a linked list of free memory address, is naturally discontinuous, and the chain of the list of traversal direction is from the low address to high address. The size of the heap is limited by the valid virtual memory in the computer system. Thus, the space of the heap is more flexible and relatively large.
Fragmentation Issues:
in the case of heaps, frequent New/delete is bound to cause memory space discontinuity, resulting in a large number of fragments, so that program efficiency is reduced. For the stack, there is no problem, because the stack is advanced out of the queue, they are so one by one correspondence, so that there will never be a memory block from the middle of the stack popped
Allocation method:
2 alloca The function is assigned, But the dynamic allocation of stacks is different from the heap, and his dynamic allocation is released by the compiler without our manual implementation.
allocation efficiency:
The stack is the data structure provided by the machine system, the computer will support the stack at the bottom: allocate the address of the special register storage stack, the stack stack has special instruction execution, which determines the efficiency of the stack is high. The heap is provided by the C + + function library, and its mechanism is very complex.
Differences in heap and stack in iOS