<>C/C++ Memory management must know and know
<>1.C Three memory allocation methods for languages
* Static storage allocation
* On stack allocation
* Heap allocation
Static storage areas are also called global data areas , Contains many data types , With global variables , Static variable , General constants , Character constants, etc . Its memory allocation exists throughout the program , It will not be released until the existing program is completed .
Stack area , Used to store parameter values of functions , Values of local variables, etc . When the function is executed , The storage unit of local variables inside the function will be created on the stack , After the function execution is completed , These storage units are automatically released .
Heap area , Generally assigned and released by the programmer , If the programmer does not release , After the program runs, it will be automatically recycled by the operating system .malloc and free This memory is what the function operates on .
<>2. The difference between heap and stack
The main differences between heap and stack are :
* Memory application method
* Stack : Automatically assigned by the system . For example, when declaring a local variable of a function int b, The system automatically sets b Open up space .
* heap : Programmers need to apply by themselves .C In language malloc function , Size to be specified ;C++ use new operator , You don't need to specify the size yourself
* Memory request size limit
*
Stack : Stack space is limited . The address at the top of the stack and the maximum capacity of the stack are preset by the system , stay windows The size of the lower stack is 2M. If the requested space exceeds the remaining space of the stack, you will be prompted overflow.
* heap : The heap is a large free storage area , The space is much larger than that of the stack , Its size is limited by the virtual memory available in the computer system .
* Data structure of memory
* Stack : Stack is a data structure extending from high address to address , Is a contiguous area of memory .
*
heap : Heap is a high address extended data structure , And discontinuous memory area . This is because the system uses a linked list to store free memory addresses , Nature is discontinuous , And the traversal direction of the linked list is from low address to high address . When the system receives the application of the program , Will traverse the linked list , A heap node with a space larger than the requested space is required , Then delete the node from the memory free node list , Allocate the space of this node to the program .
* Application efficiency
* Stack : Stack is automatically allocated by the system , Faster , Programmer has no control .
* heap : Heap is used by programmers malloc or new To apply voluntarily , The system will allocate space after receiving the application . Its speed is relatively slow , And easy to generate memory fragments .
* Store content
*
Stack : On function call , The first thing on the stack is the address of the next instruction in the main function ( Next executable statement of function call ), Then each parameter of the function is put on the stack , stay C In the language compiler , Function parameters are stacked from right to left ; Then there are the local variables of the function . Note that static variables are not stacked .
* heap : Generally, a byte is used at the head of the heap to store the heap size . The specific contents in the heap are arranged by the programmer himself .
<>4. C Language parameter stack order
Stack pressing sequence is from right to left .
This stack method has the advantage that the number of parameters can be dynamically changed . According to stack analysis , Stack from left to right , The first parameter is pressed at the bottom of the stack . Unless you know the number of parameters , Otherwise, the leftmost parameter cannot be obtained from the relative displacement of the stack pointer . Thus, the number of parameters on the left is uncertain , Just opposite to the number of dynamic parameters . Therefore, the main reason for using this stack method is to support the variable length parameter form .
<>5. C The role of stack in language
*
C Using stack to store temporary variables in language , Temporary variables include function parameters and temporary variables defined inside a function . In a function call , Function return address related to function call , Temporary variables in functions , Registers are stored on the stack .
*
Stack is the foundation of multithreaded programming , footstone . Each thread has at least one dedicated stack , It is used to store the temporary variables of various functions during the running of this thread and protect the on-site running scenarios . The most basic function of the operating system is to support multi-threaded programming , Support interrupt and exception handling , Interrupt and exception handling also have proprietary stacks , Therefore, stack is also the cornerstone of operating system multithreading management .
<>6.C++ memory management
* Stack area (stack): Automatically allocated and released by the compiler , Store function parameter values , Values of local variables, etc . It operates like a stack in a data structure .
* Heap area (heap): Generally assigned and released by the programmer , If the programmer does not release , May be recycled by the operating system at the end of the program . It is different from the heap in the data organization , The allocation method is similar to the linked list .
*
Global area ( Static area )(static): Global variables and static variables are stored together , Initialized global variables and static variables are in the same area , Uninitialized global variables and uninitialized static variables are in another adjacent area . Released by the system after the program ends .
* Text constant area : Constant string is put here . Released by the system after the program ends .
* Program code area : Binary code for storing function body
And in C++ in , Virtual memory can be divided into code segments , Data segment ,BSS paragraph , Heap area , File mapping area and stack area
* Code snippet : Including read-only storage area and text area , Read only memory stores string constants , The text area stores the machine code of the program
* Data segment : Global and static variables that have been initialized in the stored program
* BBS paragraph : Store uninitialized global and static variables , And all are initialized to 0 Global and static variables for
* Heap area : call new/malloc Dynamically allocate memory in heap when , Also call delete/free To manually free the requested memory
* File mapping area : Storing dynamic link libraries and calls mmap function ( Memory mapping function ) File mapping performed
* Stack : Use stack space to store function return address , parameter , local variable , Return value
<>7. Memory leak
<>7.1 What is a memory leak
Memory leak simply put , Is to apply for a piece of memory , It is not released after use .
Memory leakage usually occurs when the program runs longer , Using more and more memory , Will eventually run out of memory , Cause the whole system to crash . If the program requests a piece of memory , Without any pointer to it , Manage it , Then this memory leaks .
<>7.2 Detect and avoid memory leaks
In fact, the causes of memory leakage can be summarized as : Called malloc/new Wait for the operation of memory request , But the corresponding free/delete.
Ways to avoid memory leaks can be summarized :
* Programmers should develop good programming habits , ensure malloc/new and free/delete matching
* Self manage the pointer of allocated memory in the form of linked list , Delete from the linked list after use , At the end of the program, you can check whether there is memory in the linked list that has not been released
* Use some tool plug-ins for checking malloc/new and free/delete Match or not
* apply Boost Smart pointer in
Technology