C language pointers and memory leaks
Common Memory Errors:
1. the memory allocation was unsuccessful but it was used.
if pointer is the parameter of the function, to be used at the entrance of the function ( p Span style= "font-size:16px;font-family: ' The song Body '; > =null ) for inspection;
if it is used malloc to dynamically request memory, you should use if ( P==null ) or if ( P ! =null) for error-proof handling.
2. the memory allocation was successful and was not initialized before it was used.
3. the memory allocation succeeds and initializes, but the operation is out of bounds.
For example, the use of arrays often occurs when "multiple 1 "or" less 1 "action.
4. forgetting to release memory causes a memory leak.
in general, we often say that memory leaks refer to the leakage of heap memory. Heap memory is the memory that a program must explicitly release after it is allocated dynamically from the heap. Applications typically use malloc,calloc,realloc , and so on to allocate a chunk of memory from the heap, and after use, call Free Release the memory block, or the memory can not be called again, this memory is leaked inside.
Scenarios that lead to memory leaks:
1. re-assign value
Char*memoryarea = malloc (10);
Char*newarea = malloc (10);
Memoryarea and the Newarea are assigned separately. Ten bytes, as shown in. If you execute a statement such as the following (pointer re-assigned value)
Memoryarea= Newarea;
in the preceding code statement, the developer will Memoryarea The pointer assigns a value to Newarea pointer. As a result, The memory location previously pointed to by Memoryarea becomes orphaned, as shown in the following figure. It cannot be freed because there is no reference to that location. This results in a memory leak of ten bytes.
Make sure that the memory location does not become orphaned before you assign a value to the pointer.
2. First release the parent block
Suppose there is a pointer Memoryarea , it points to a Ten the memory location of the byte. The third byte of the memory location points to a dynamically allocated memory location of ten bytes.
free (memoryarea)
If you call free to release memoryarea newarea NEWAREA  NEWAREA 
whenever a structured element is released, and the element contains a pointer to a dynamically allocated memory location, the child memory location should be traversed first (in this case , Newarea ), and from there, start releasing, and then traverse back to the parent node.
The correct implementation here should be:
Free (Memoryarea->newarea);
Free (Memoryarea);
3. Incorrect handling of return values
Sometimes, some functions return a reference to dynamically allocated memory
Char*func ()
{
return malloc (20);
}
Voidcallingfunc ()
{
Func ();
}
in the example above, Callingfunc () function in the func () The call to the function did not process the return address of the memory location. As a result,the three-byte block allocated by thefunc () function is lost and causes a memory leak.
C Language Common Memory error