Interview questions - the difference between stack and heap

The difference between stack and heap

In the computer field, the stack can not be ignored is a concept, we basically written in C language program should be used. But for many of the beginner, the stack is a very vague concept. Stack:

  • A data structure.
  • For a place to store the program is running.

Stack and heap data structures

  • Is a stack data structure having a LIFO properties linear form, that is taken after the first storage, taken after the first storage. This is like what we want out on the bottom of the box inside the (relatively early into objects), we must first remove the pressure on it objects (objects into relatively late).
  • Stack is implemented using an array of the binary tree, the parent does not use all of its child pointer or pointers. Heap "heap property" as sorted by "heap property" determines the position of the nodes in the tree.
    Reference heap structure: https://www.jianshu.com/p/6b526aa481b1

Memory stack

  • Stack allocation of local variables, temporary memory space variables, memory stack area in order to address the relatively high growth direction of the address is on a relatively small stack of memory, so open up too much can cause a stack overflow ( For example, when using recursion, recursive layers too deep or not recursive termination conditions may result in a stack overflow).
  • Heap is growing up memory space allocated for application programmers, such as malloc and out of the new space is on the heap, the characteristics of these variables heap is manually opened and manual release, failed to timely release may cause memory leak.

Which heap and stack which access faster?
Allocation and deallocation, when allocating and releasing heap should call the function (MALLOC, FREE), for example, will be to find enough space to heap size of the allocation (because after several release distribution can cause voids), which will take some time, you can look at specific MALLOC and FREE source code, they do a lot of extra work, but the stack does not need these. To sum up, stack efficient than heap

Published 90 original articles · won praise 56 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_37266079/article/details/104351199