C++ memory management problems:
Stack overflow:
Stack memory allocation:
Stack: During the execution of a function, storage units of local variables in the function can be created on the stack, and these storage units are automatically released at the end of the function execution. Stack memory allocation operations are built into the processor’s instruction set, which is highly efficient, but the allocated memory capacity is limited.
Only when the stack space is larger than the requested space, the system will provide memory for the program, otherwise there will be stack overflow, that is, overflow problem.
Heap overflow:
Heap memory allocation:
The heap is the block of memory allocated by malloc, which the compiler does not care about, but is controlled by our application. Usually a malloc corresponds to a free. If the programmer does not release it, the operating system automatically recycles it when the program ends. The memory allocated by new and DELETE is in free storage, which is typically implemented by the heap.
If you continue to apply for space without using DELETE to release space, the memory will continue to grow, that is, a memory leak will occur.
The difference between heap and stack:
-
Different management methods: for the stack, it is automatically managed by the compiler, without our manual control; In the case of the heap, the release is controlled by the programmer, leading to memory leaks.
-
Space size: For heap memory, generally up to 4G on 32-bit systems, almost unlimited; For stacks, there is usually a space limit.
-
Fragmentation problem: For the heap, frequent new/ DELETE will inevitably lead to discontinuous memory space, resulting in a large amount of fragmentation, making the program inefficient. And because stacks are first in, last out queues, there is no space fragmentation problem for stacks.
-
Growth direction: For the heap, the growth direction is upward, that is, toward the direction of increasing memory address; In the case of the stack, the growth direction is downward, in the direction of memory address decrease.
-
Allocation: The heap is dynamically allocated, there is no statically allocated heap. There are two types of stack allocation: static allocation and dynamic allocation. Static assignment is done by the compiler, such as assignment of local variables. Dynamic allocation is done by the Alloca function, but stack dynamic allocation is different from heap dynamic allocation, which is released by the compiler without manual implementation.
-
Allocation efficiency: the stack is a data structure provided by the machine system, and the computer provides support for the stack at the bottom: special registers are allocated to store the address of the stack, and special instructions are executed for pushing and removing the stack, which determines the high efficiency of the stack. Heap is C/C + + library, its mechanism is very complex, for example in order to allocate a block of memory, library functions will be according to certain algorithm (specific algorithm can reference data structure/operating system) in the heap memory search available enough space, the size of size if there is not enough space (which may be due to too much memory fragments), It is possible to call system functions to increase the memory space of the program’s data segment, so that it has a chance to be allocated enough memory and then returned. Obviously, the heap is much less efficient than the stack.
Common errors in memory allocation and countermeasures
-
Memory was not allocated but used
Check whether the pointer is NULL before using memory. If the pointer p is an argument to the function, use assert(p! =NULL) for checking. If (p==NULL) or if(p! =NULL) for error prevention.
-
Used without initialization after successful memory allocation
Reasons: 1. There is no concept of initialization. 2. The default memory values are all 0, which leads to incorrect initial values. Solution: Initialize before use
-
Memory allocated successfully and initialized, but out of bounds
-
The used memory is not released, causing a memory leak
If the applied memory is not released after it is used up, the memory gradually becomes smaller and eventually runs out of memory, leading to program crash. Dynamic memory must be applied and freed in pairs, malloc and free must be used the same number of times, as well as new and DELETE, otherwise errors will inevitably occur.
-
Continued use after memory is freed (wild pointer)
-
The object call relationship in the program is too complicated, it is difficult to know whether an object has freed the memory, at this time, we should redesign the data structure, fundamentally solve the confusion of object management.
-
The function’s return statement was incorrectly written. Be careful not to return a pointer or reference to the stack memory, because the memory is destroyed at the end of the function body.
-
Pointer is not set to NULL after memory is freed using free or DELETE. The “wild pointer” is generated.
-
Why do we need new/delete when we have malloc/free
Malloc and free are C/C++ library functions, while new and DELETE are C++ operators used to dynamically allocate and free memory.
For non-internal data type objects, malloc and DELETE alone are not sufficient for dynamic objects. An object needs to execute its constructor automatically when it is created, and an object needs to execute its destructor automatically before it dies. Since malloc and Free are library functions and not operators, the compiler does not have permission to impose the task of executing constructors and destructors on Malloc and Free. For internal data types, the two are equivalent.
New and DELETE are not library functions; they are operators.
The difference between new and malloc memory allocation failure
-
New will throw an exception if it fails to allocate enough memory. The default action is to throw a bad_alloc exception when new cannot allocate enough memory.
-
Malloc memory allocation failure does not raise an exception, but only returns a NULL value.
Reference [] (https://www.cnblogs.com/findumars/p/5180490.html)