Five partition memory

First, prior knowledge - memory allocation procedure  
  consisting of C / C ++ compiler program memory occupied into the following sections  
  1, stack area (stack): released automatically allocated by the compiler, the storage function parameters, local variables value and so on. Its  
  operation is similar to the data structure of the stack.  
  2, the heap (heap): general distribution release by the programmer, if the programmer does not release, possibly by the end of the program back to the OS  
  income. Note that it is a stack data structure different things, touches similar distribution list, Oh.  
  3, the global zone (quiescent zone) :( static) and the global variable is stored in a static variable, and initializing the  
  global and static variables in an area, uninitialized global variables and static variables uninitialized adjacent another  
  one area. After the end of the program released by the system.  
  4, literals area: constant string is placed here. After completion of the program is released from the system  
  5, the program code area: storing binary code function body.  
   
   
  Second, the example program    
  is very detailed examples

  // main.cpp     
  int    A =    0 ;    // global initialization region     
  char    * P1;    // global zone uninitialized     
  main ()     
  {     
  int    B;   // stack     
  char    S [] =    " ABC " ;    // stack     
  char    * P2 ;    // stack     
  char    * p3 =    " 123456 " ;    // in the constant region, on 123456/0 p3 stack.    
  static    int    C = 0 ;    // global (static) zone initialization    
  = P1 ( char    *) the malloc ( 10 );     
  P2    = ( char    *) the malloc ( 20 ); // allocate 20 bytes and 10 have come in the area of the stack area.    
  strcpy (p1,    " 123456 " );    // 123456/0 put constant region, the compiler might p3 points to it with "123456" optimization into one place.    
  }   

 

   
  Second, the theoretical knowledge of the heap and stack    
  2.1 mode of application    
  stack:    
  assigned automatically by the system. For example, in the function declaration of a local variable int b; b is automatically opened empty stack  
  between    
  heap:    
  requires application programmer, and indicate the size, in c, the malloc function    
  as p1 = (char *) malloc ( 10) ;    
  in C ++ using the new operator    
  as p2 = new char [10];    
  it is noted that p1, p2 in the stack itself.    
   
   
  2.2    
  After the application system response    
  stack: As long as the remaining stack space is greater than the application space, the system will provide program memory, otherwise it will report an exception in the stack overflow  
  out.    
  Heap: you should know that the operating system has a list of free memory address of the record, when the system receives the application process,  
  will traverse the linked list, find the first space is greater than the application heap space node, then the node from free node list  
  to delete, and assign the node to the space program, in addition, for most systems, this memory space will be in the  
  first address at the record size of this allocation, so, delete the statement in the code the right to release 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 excess portion that  
  points back into the free list.    
   
  2.3 application size limit    
  Stack: In Windows, the stack is expanded to the low address data structure is a contiguous area of memory. Meaning of this sentence  
  thinking is the maximum capacity of the stack address and stack predetermined system is good, in WINDOWS, the stack size is 2M (also  
  said to be 1M, in short, to determine a compile-time constant), If space applications exceeds the remaining space on the stack will  
  prompt 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, of course is not continuous, and the list traversal direction is from low to higher addresses. Heap size  
  is limited by the computer system effective virtual memory. Thus, the space available heap more flexible, is relatively large.    
   
   
   
  2.4 Comparison of the efficiency of application:    
  Stack is automatically assigned by the system faster. But the programmer can not control.    
  Heap is a new memory allocation, generally more slowly, and prone to memory fragmentation, but it most convenient to use.    
  In addition, in WINDOWS, the best way is to use VirtualAlloc to allocate memory, he was not in the heap, not the stack It is  
  to retain a memory address space directly in the process, although it used the most inconvenient. But the speed, but also the most flexible.  
     
   
  2.5 heap and stack memory contents    
  Stack: the function call, the first into the stack is the next instruction in the main function (the function call may be a statement  
  to execute a statement) address and is a function of various parameters in most of the C compiler, parameters are pushed onto the stack from right to left  
  , And it 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 of the memory  
  address, 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.    
   
  2.6 Comparison access efficiency    
   
  char S1 [] = "aaaaaaaaaaaaaaa";    
  char * S2 = "bbbbbbbbbbbbbbbbb";    
  aaaaaaaaaaa at run-time assignment;    
  and bbbbbbbbbbb is determined at compile time;    
  however, after the access, array on the stack than strings (e.g., stack) pointer points.    
  For example:    
  #include    
  void main ()    
  {    
  char. 1 = A;    
  char C [] = "1234567890";    
  char * P = "1234567890";    
  A = C [. 1];    
  A = P [. 1];    
  return;    
  }    
  corresponding to assembly code    
  10: A = C [. 1];    
  00,401,067. 8A 4D Fl MOV Cl, byte PTR [EBP-0Fh]    
  0040106A 88 4D the FC MOV byte PTR [EBP-. 4], Cl    
  . 11: A = P [. 1];    
  0040106D 8B 55 EC EDX MOV, DWORD PTR [EBP-14H]    
  00.40107 million. 8A Al MOV 42 is 01, byte PTR [EDX +. 1]    
  00,401,073 88 45 PTR the FC byte MOV [EBP-. 4], Al    
  first string when read directly put the elements of the read register cl, while the second will have to first read pointer value  
  edx, and then reading character according edx, apparently slow.    
   
   
  2.7 Summary: The    
  difference between heap and stack can be used as a metaphor of view:    
  use the stack as we go to a restaurant for dinner, just a la carte (issued application), pay, and eat (use), fed on the  
  go, without regard to vegetable, vegetables and other preparation and washing dishes, scrubbing pots, etc. off the work, his advantage is fast, but from  
  a small degree.    
  Heap is like a DIY like to eat the food, too much trouble, but more in line with their own tastes, and freedom  
  degree.  

Guess you like

Origin www.cnblogs.com/yuanmingzhou/p/11222659.html