Daily Question 2: The difference between heap (heap) and stack (stack) of

  Because there is no means clear that the heap data structures or storage, so the two attempt to answer this question.

  A. Heap and stack data structure as

  1. heap (heap), also called a priority queue (priority queue), the operation is allowed in the queue FIFO (the FIFO), the element is inserted in the tail removed HOL elements. The stack is the same, the element is inserted in the stack bottom, the top of the stack elements removed, but the arrangement of elements in the heap is not in accordance with the order of arrival, but arranged according to a certain priority order. The priority rules may be the size or other elements.

  2. Stack (Stack), a limited linear operation table, last-out stack when allowed operations (FILO), insert elements in the stack, remove the top of the stack elements. Also, it only refers to stack together a stack, that stack refers to a stack.

  II. Heap and stack as storage

  1. stack area, when needed means allocated by the compiler, the storage area is automatically removed when not needed variables. Variables which are typically local variables, functions and other parameters. In one process, located at the top of the user virtual address space is the user stack, the compiler uses to achieve the function was called. And the stack, users stack may expand and contract to dynamically during program execution.

  2. heap, dispensing means rather than by the new compiler automatically allocated memory block, controlled by the application, generally corresponding to a new one must delete. If a programmer is not freed, then at the end of the program, the operating system will automatically recover. Heap can dynamically expand and contract.

  Detailed differences:

    

  The reasons for these differences:

  Growth direction: Stack: In Windows, the stack is expanded to the low address data structure is a contiguous area of memory. This means that the maximum capacity of the stack address and stack predetermined system is good, in WINDOWS, the stack size is 2M (some say 1M, in short, it is a compile-time constants determined), if when space applications exceeds the remaining space on the stack you will be prompted to overflow. Therefore, less space obtained from the stack.

  Heap: heap is extended to the high-address data structure is not continuous memory area. This is because the system is a linked list of free memory for storing the address of a discontinuous nature, and the list traversal direction is from low to higher addresses. The heap size is limited by the computer system effective virtual memory. Thus, the space available heap more flexible, is relatively large.

  Dispensing efficiency: the stack is a data structure of the machine provided by the system, the computer will provide the underlying support for a stack: a special register storage allocated address stack, the stack has a dedicated push instruction is executed, which determines the efficiency of the stack is relatively high . Stack is the C / C ++ library provided, its mechanism is complicated, for example, in order to allocate a memory library functions will follow a certain algorithm (specific algorithm may reference a data structure / operating system) search heap memory available space of sufficient size, if there is no space of sufficient size (probably due to too many fragmented memory), it is possible to call the system function to increase the memory space program data segment, so there is ample opportunity to be assigned to the size of the memory, and then return. Obviously, heap much lower efficiency than the stack.

 

 


————————————————

Reference Bowen: HTTPS: //blog.csdn.net/qq_35637562/article/details/78550953  https://www.cnblogs.com/youxin/p/3313288.html

Guess you like

Origin www.cnblogs.com/honernan/p/12177981.html