The difference between stack and heap memory area stack area data structure _ reproduced

Heap characteristics:
must be a complete binary tree
with the array to achieve
the value of any of a node is the maximum of all its sub-tree nodes or minimum value
when the maximum value, known as the "maximum heap", also known as large root stack;
In a complete binary tree, the maximum value of any sub-sub-tree root node of the tree are in this.

When the minimum value, known as the "minimum heap", also known as the small root heap.
In complete binary tree, a minimum value of any sub-sub-tree root node of the tree are in this.
Large root heap

 

Small heap root

 

It can be seen for the stack ** (Heap) * This data structure, from the root node to all nodes on any node paths are ordered.
Finishing :
binary heap
Logically: complete binary tree 
stored: an array (sequential storage) 
action: to find the most value (the priority queue)
Operation :
Downward adjustment * (the construction team, delete) 
to build the heap 
adjusted upward (insert)
Difference data structure memory heap and the heap area
a, the data structure of the heap and stack
   
Stack and heap are two different data structures in the data structure. Data structure of both the data item in sequence. 
   Stack: like a bucket or box loaded data 
    stack is more familiar to a data structure, it is a kind of LIFO data structure, which means that after the deposit of the first take, 
    after the first deposit is taken, which is similar to we want to get at the bottom of a box of things (objects into them earlier), 
    we must first remove the pressure on it objects (objects into relatively late). 
   Heap: like an inverted tree 
   stack through an ordered tree data structure, each node has a value. Usually what we call stack data structure refers to the binary tree. 
   Characterized heap root value minimum (or maximum), and the tree is a root node of the two stacks. 
   Because of the heap this feature, used to implement priority queues, heap access is random, just as we take books on library shelves, although placing the book is in order, 
   but we want to take any one do not like the stack as when, first remove all the previous books, bookshelves this mechanism is different from the case, we can remove the book we want to direct.
Second, the memory allocation in the heap and stack
   
We are not frequently used data structures in the heap and stack, the reason that the data structure is to heap and stack and heap later to say the difference between open and stack area, please be sure to pay attention. 
   Stack memory address region at a relatively high, to the growth direction of the upper address, then the address stack grows downwards. 
   Allocating stack space for local variables, the heap area is increased up to the application programmer memory space allocation. 
   There is also a static area is assigned a static variable, the global variable space. 
   Read-only area is constant and the distribution of the program code space; and other partitions.
Look at a classic example:
main.cpp 
   int A = 0; global initialization region 
   char * p1; uninitialized global zone 
   main () 
   { 
   int B; stack 
   char s [] = "abc" ; stack 
   char * p2; stack 
   char * p3 = "123456"; 123456 \ 0 in the constant region, p3 in the stack area 
   static int c = 0; global (static) zone initialization 
   p1 = (char *) malloc ( 10); stack 
   p2 = (char *) malloc ( 20); stack 
   }
Third, the difference between the stack area in memory allocation and heap regions
different 0, the mode of application and recovery methods
 
 I do not know if you understand a little bit of a difference between stack and heap is a different way of application: Stack (English name; stack) is automatically allocate space
1
, for example, we define a char a; the system will automatically open up its space on the stack. The stack (English name: heap) is a programmer needs its own space applications, such as malloc (10); byte space is opened up. Because space on the stack is automatically assigned automatic recovery, so the lifetime of the data on the stack just in the process of running the function, run out after the release, can no longer access. The heap of data as long as the programmer does not free up space, you can have access to, but the disadvantage is that you forget the release will be a memory leak.
1 response after filing system
    Stack: As long as the stack space than the remaining space application, the system will provide program memory, otherwise it will report an exception in the stack overflow. 

 Heap: First of all you should know the operating system has a linked list of free memory address of record, when the system is subject to the application process, will traverse the linked list, find the first space is greater than the application heap space. 
 Node, then the node is deleted from the free list node and assign the node to the space program, 
 In addition, for most systems, this will address the first memory space allocated to this recording size, so that the code can correct delete statement released this memory space. 
 Further, since the size of the size of the stack to find the node is not necessarily exactly equal to the application, the system will automatically back into the excess portion of the free list. 
 That pile will need to do some follow-up work after applying this application will lead to efficiency problems
2, application efficiency comparison
Stack: automatically assigned by the system faster. But the programmer can not control. 
Heap: It is a new memory allocation, usually more slowly, and prone to memory fragmentation, but it most convenient to use.
3, application size limit
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 used to store the linked list of free memory address is 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.
4, heap and stack memory contents
 
Due to size limitations of the stack, so a subroutine or physically meaningful, not just the logical sense. 
Stack: the function call, the stack has a first next instruction after the function call in the main function (the function call statement surprised executable statements) address, 
and is a function of various parameters, in most C compiler, the parameter is right to left in the stack, and is a function of the local variables. 
Note that the static variable is not pushed on. 
After the end of this function call, first-out stack of local variables, then the parameters of the last stack pointer points to the beginning address of the memory, 
i.e. the main function of the next instruction, the program continues to run from this point. 
Heap: one byte is generally stored in the heap size of the head stack. The specific content of the heap arranged by the programmer.
5, heap and stack on a more vivid metaphor
Stack: Use the stack as we go to a restaurant for dinner, just a la carte (issued application), pay, eat (use), fed and left, 
without regard to vegetable, vegetables and other preparation and washing dishes, scrubbing pots such as mop-up work, his advantage is fast, but little freedom. 
   Heap: heap is like a DIY like dishes, too much trouble, but more in line with their own tastes, and the large degree of freedom
The basic operation of the stack
heap is a complete binary tree

 


The complete binary tree is actually an array, you can think of it as a complete binary tree. Define a rule in the array,
Rule
left 2i + 1  
Right 2i + 2
parent (i-1) / 2
large root stack downward adjustment
Every time the array comes in a tree, comparing it with the parent node, if he is greater than, exchanged 
find a parent node, the incoming number in the subscript of the array (i-1) / 2, is the parent node s position
Stack operation of
the stack downward adjustment
 
 

 

 

Built heap
establish small pile of

 

From the last non-leaf node, we continue to adjust downwards.
Upward adjustment
 

 Transfer: https: //blog.csdn.net/liuyuchen282828/article/details/89255389

         
         
                 
 
 
       

Guess you like

Origin www.cnblogs.com/henuliulei/p/10962578.html