Difference Between Heap and Stack

Original link: https://blog.csdn.net/hairetz/article/details/4141043/

1. Memory allocation

stack: automatically allocated and released by the compiler to store the parameter values ​​of functions and the values ​​of local variables.

heap: Generally allocated and released by the programmer, if not released, it may be reclaimed by the OS when the program ends.

static: Global variables and static variables are stored together. Initialized global variables and static variables are in one area, and uninitialized ones are in an adjacent area. They are released by the system after the program ends.

Text constant area: The constant string is placed here, and is released by the system after the program ends.

Program code area: store the binary code of the function body.

 
 
//main.cpp    
  int a = 0; //global initialization area    
  char *p1; //Global uninitialized area    
  main()    
  {    
  int b; //stack    
  char   s[]   =   "abc";   //栈    
  char *p2; //stack    
  char *p3 = "123456"; //123456/0 is in the constant area, p3 is on the stack.    
  static int c = 0; //Global (static) initialization area    
  p1   =   (char   *)malloc(10);    
  p2 = (char *)malloc(20); //The allocated area of ​​10 and 20 bytes is in the heap area.    
  strcpy(p1, "123456"); //123456/0 is placed in the constant area, the compiler may optimize it into a place with "123456" pointed to by p3.    
  }    

2. Theoretical knowledge

 (1) Application method: stack, the system will automatically assign. heap: The programmer applies by himself and specifies the size.

 (2) System response after application: 

 Stack: As long as the remaining space of the stack is larger than the requested space, the system will provide memory for the program, otherwise an exception will be reported to indicate stack overflow.

 Heap: First of all, you should know that the operating system has a linked list that records free memory addresses. When the system receives an application from a program, it   will traverse the linked list to find the first heap node whose space is larger than the requested space, and then remove the node from the heap. Delete from the free node list and allocate the space of the node to the program. In addition, for most systems, the size of this allocation will be recorded at the first address in this memory space. In this way, the delete statement in the code In order to correctly release the memory space. In addition, since the size of the found heap node is not necessarily exactly equal to the size of the application, the system will automatically put the excess part   back into the free list.

(3) Restrictions on application size:

Stack: Under Windows, the stack is a data structure that extends to a lower address and is a contiguous memory area. This sentence means   that the address of the top of the stack and the maximum capacity of the stack are predetermined by the system. Under WINDOWS, the size of the stack is 2M (  some say 1M, in short, it is a constant determined at compile time), If the requested space exceeds the remaining space of the stack, overflow will be prompted. Therefore, less space can be obtained from the stack.    

Heap: The heap is a data structure that extends to high addresses and is a discontinuous area of ​​memory. This is because the system uses a linked list to store free memory addresses, which are naturally discontinuous, and the traversal direction of the linked list is from low addresses to high addresses. The size of the heap is limited by the virtual memory available in the computer system. It can be seen that the space obtained by the heap is more flexible and larger. 

(4) Comparison of application efficiency:

  Stack: The system automatically allocates, and the speed is faster. But the programmer is out of control.    

  Heap: The memory allocated by new is generally slower and prone to memory fragmentation, but it is the most convenient to use.    

(5) Storage contents of heap and stack:

 Stack: When a function is called, the first thing pushed into the stack is the address of the next instruction after the main function (the next  executable ), and then the various parameters of the function. In most C compilers , the parameters are pushed onto the stack from right to left , followed by the local variables in the function. Note that static variables are not pushed onto the stack. When this function call ends, the local variables are popped off the stack first, then the parameters, and finally the top of the stack pointer points to the first stored address     , which is the next instruction in the main function, and the program continues to run from this point.

 Heap: Generally, one byte is used to store the size of the heap at the head of the heap. The specific contents of the heap are arranged by the programmer.

(6) Comparison of access efficiency

During initialization, the stack is assigned directly, and the stack is assigned at compile time, but for subsequent access, the stack is faster than the stack.

Summary:  Using the stack is like when we go to a restaurant to eat, just order food (issue an application), pay, and eat (use), and leave , without worrying about the preparations such as chopping and washing vegetables, and washing dishes and brushing. The advantage of the pot and other finishing work is that it is fast, but the degree of freedom small. Using the heap is like making your own favorite dishes. It is more troublesome, but it is more in line with your own taste and has a large degree of freedom . (classic!)  


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325483791&siteId=291194637