Preliminary knowledge – program memory allocation



The memory footprint of a C/C++ compiled program is divided into the following sections



1. Stack area (stack) – Automatically allocated by the compiler to store function parameter values, local variable values, etc. its



Operates like a stack in a data structure.



2. Heap — Usually allocated and released by the programmer. If not released by the programmer, the program may be returned by the OS at the end



Closed. Note that this is not the same thing as a heap in a data structure, but is distributed like a linked list.



Global (static) — Global and static variables are stored together and initialized



Global variables and static variables are in one area, and uninitialized global variables and uninitialized static variables are in adjacent areas



An area. – Released by the system after the program ends.



4. Literal constants – This is where constant strings are placed. The program is released by the system after completion



5. Program code area – store the binary code of the function body.



   



   



Two, example procedures



It was written by a senior, in great detail



  //main.cpp    



int a = 0; Global initialization area



char *p1; Global uninitialized region



  main()    



  {    



int b; The stack



char s[] = “abc”; The stack



char *p2; The 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 10 – and 20-byte regions allocated are in the heap.



strcpy(p1, “123456”); 123456/0 is placed in the constant area. The compiler may compare it with “123456” pointed to by p3.



Optimized into one place.



  }    



   



   



2. Theoretical knowledge of heap and stack



2.1 Application Methods



  stack:    



It is automatically assigned by the system. For example, declare a local variable int b in a function; The system automatically creates a void in the stack for B



between



  heap:    



Requires the programmer to apply, and specify the size, in C malloc function



P1 = (char *)malloc(10);



Use the new operator in C++



P2 = new char[10];



But notice that P1 and P2 are themselves on the stack.



   



   



  2.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 indicating stack overflow



And out the other.



Heap: The first thing you should know is that the operating system has a linked list of free memory addresses. When the system receives a request from a program,



The list is traversed, looking for the first heap node with more space than the requested space, and the node is linked from the free list



And allocates space for this node to the program, and, for most systems, in this memory space



The size allocated is recorded at the beginning of the address, so that the delete statement in the code can correctly free the memory space.



In addition, since the size of the found heap may not be exactly the size of the requested heap, the system automatically removes the extra heap



Put it back into the free linked list.



   



2.3 Limit of application size



Stack: Under Windows, a stack is a low-address data structure, a contiguous area of memory. The meaning of this sentence



The address of the top of the stack and the maximum size of the stack are predetermined by the system. Under WINDOWS, the stack size is 2M (also available)



If the amount of space requested exceeds the amount of space left on the stack



Prompted to overflow. Therefore, less space can be obtained from the stack.



Heap: A heap is a data structure that extends to a high address and is a discontinuous area of memory. This is because the system is stored in linked lists



Is naturally discontinuous, and the traversal direction of the linked list is from low address to high address. The size of the heap



Limited by the virtual memory available in a computer system. Thus, the heap is more flexible and larger.



   



   



   



2.4 Comparison of application efficiency:



The stack is automatically allocated by the system, which is fast. But programmers have no control.



The heap is the memory allocated by new, which is generally slow and prone to fragmentation, but is the most convenient to use.



In addition, on WINDOWS, the best way to allocate memory is to use VirtualAlloc, which is not in the heap or on the stack



Save a chunk of memory directly in the process’s address space, even if it’s the least convenient. But it’s the fastest and most flexible.



     



   



2.5 Storage contents in the heap and stack



Stack: When a function is called, the first instruction to be pushed is the next instruction in the main function



Execute statement), followed by the function’s arguments, which in most C compilers are pushed from right to left



And then the local variables in the function. Note that static variables are not pushed.



When the call is complete, local variables are removed from the stack first, then parameters, and finally a pointer to the top of the stack points to the original location



Address, the next instruction in the main function, from which the program continues to run.



Heap: The size of the heap is typically stored in one byte at the head of the heap. The specific contents of the heap are arranged by the programmer.



   



2.6 Comparison of access efficiency



   



  char   s1[]   =   “aaaaaaaaaaaaaaa”;    



  char   *s2   =   “bbbbbbbbbbbbbbbbb”;    



Aaaaaaaaaaa is assigned at run time;



BBBBBBBBBBBBB is determined at compile time;



However, in later access, the array on the stack is faster than the string to which the pointer points (such as the heap).



Such as:



  #include    



  void   main()    



  {    



  char   a   =   1;    



  char   c[]   =   “1234567890”;    



  char   *p   =”1234567890″;    



  a   =   c[1];    



  a   =   p[1];    



  return;    



  }    



Corresponding assembly code



  10:   a   =   c[1];    



  00401067   8A   4D   F1   mov   cl,byte   ptr   [ebp-0Fh]    



  0040106A   88   4D   FC   mov   byte   ptr   [ebp-4],cl    



  11:   a   =   p[1];    



  0040106D   8B   55   EC   mov   edx,dword   ptr   [ebp-14h]    



  00401070   8A   42   01   mov   al,byte   ptr   [edx+1]    



  00401073   88   45   FC   mov   byte   ptr   [ebp-4],al    



The first reads the elements of the string directly into register CL, while the second reads the pointer value first



In edX, then read characters according to EDX, obviously slow.



   



   



2.7 summary:



The difference between heap and stack can be seen in the following analogy:



Using the stack is like going to a restaurant. We just order (send out application), pay, and eat (use), and when we’re full



Go, do not have to pay attention to cutting vegetables, washing vegetables and other preparatory work and washing dishes, brush pot and other cleaning work, his advantage is fast, but from



By degrees.



Using a pile is like making your own favorite dishes, more troublesome, but more in line with your own taste, and freedom



Degrees. (Classic!)