1. Memory Allocation Method
There are three memory allocation methods:
(1)From staticStorageRegion allocation. InnerProgramThe program has been allocated during compilation, and the program exists throughout the runtime. For example, global variables and static variables.
(2)Create on Stack. When executing a function,Local variables in the functionOfStorageUnits can be created on the stack. These storage units are automatically released when function execution ends.Stack memory allocation computation is built into the processor's instruction set, which is highly efficient, but the memory capacity allocated is limited.
(3)Allocated from the stack, also known as dynamic memory allocation. UseMalloc or new requests any amount of memoryThe programmer is responsible for releasing the memory with free or delete. The lifetime of the dynamic memory is determined by us. It is very flexible to use, but the problem is also the most.
2. Common memory errors and Countermeasures
Memory Errors are very troublesome. The compiler cannot automatically detect these errors, which can be captured only when the program is running. Most of these errors do not have obvious symptoms, but they are often invisible and increase the difficulty of error correction. Sometimes the user finds you angrily, but the program has not encountered any problems. When you leave, the error occurs again.
Common memory errors and their countermeasures are as follows:
Memory Allocation failed, but it was used.
New programmers often make this mistake because they do not realize that memory allocation will fail. The common solution is,Check whether the pointer is null before using the memory. If the pointer P is a function parameter, use assert (P! = NULL) Check. If yesIf (P = NULL) or if (P! = NULL) for error prevention.
Although the memory allocation is successful, it is referenced before initialization..
There are two main causes for this mistake: first, there is no idea of initialization; second, the default initial values of the memory are all zero, resulting in incorrect reference values (such as arrays ).
There is no uniform standard for the default initial values of the memory. Although sometimes it is zero, we prefer to trust it without any trust. So no matter whatDo not forget to assign the initial value when creating an array. Even the zero value cannot be omitted..
The memory allocation is successful and initialized, but the operation is beyond the memory boundary..
For example, when an array is used, the subscript "more than 1" or "less than 1" is often performed. Especially in for loop statements, the number of loops is easy to make a mistake, resulting in array operations out of bounds.
Forgot to release the memory, causing memory leakage.
A function containing such errors loses a piece of memory every time it is called. At the beginning, the system had sufficient memory and you could not see the error. Once a program suddenly died, the system prompts: memory is exhausted.
Dynamic Memory application and release must be paired.The usage of malloc and free must be the same, otherwise there must be an error (the same applies to new/delete).
The memory is released but it continues to be used.
There are three scenarios:
(1) The object calling relationship in the program is too complex, so it is difficult to figure out whether an object has released the memory. At this time, we should re-design the data structure to fundamentally solve the chaos of Object Management.
(2) The Return Statement of the function is incorrect. Be sure not to return the "Pointer" or "Reference" pointing to "stack memory" because the function body is automatically destroyed when it ends.
(3) After the memory is released using free or delete, the pointer is not set to null. As a result, a "wild pointer" is generated ".
Rule 1]After applying for memory with malloc or new, you should immediately check whether the pointer value is null. Prevent the use of memory with NULL pointer values.
Rule 2]Do not forget to assign initial values to arrays and dynamic memory. Avoid using uninitialized memory as the right value.
Rule 3: avoid overrunning the subscript of an array or pointer. Be careful when "more than 1" or "less than 1" is performed.
Rule 4]Dynamic Memory application and release must be paired to prevent memory leakage.
Rule 5]After the memory is released with free or delete, the pointer is immediately set to null to prevent "wild pointer".