C-language pointer error-prone
1. Memory leakage: the requested heap memory is not released.
2. Memory pollution. The following code:
If the struct contains only some underlined code, no error will be reported when it is written in the compiler, but the memory operation has already been illegal. The following will add some variables to the struct, and an error will be reported, do not allow variable definition (this error is especially difficult to debug.
3. null and invalid strings:
The blue line in the figure: the value of the pointer variable should be determined, rather than the memory pointed by the pointer.
4. pointer out of bounds: for example, str [3] = "abc ";
5. the superposition of pointers will constantly change the pointer direction.
For example, char * p = "sdfg"; p ++; printf ("% s \ n", p); printed result: "dfg ";. The pointer only needs to be changed. If it is superimposed four times, no content will be printed, because the pointer has already pointed to the Terminator.
6. Do not upload the address of the local variable in the stack area. For example:
Char * get_str () {char str [] = "abcdedsgads"; // stack, printf ("[get_str] str = % s \ n", str ); return str ;}
7. The same memory area is released multiple times. For example:
Har * p = NULL; p = (char *) malloc (50); strcpy (p, "abcdef"); if (p! = NULL) {// the function of the free () function only tells the system that the memory pointed to by p can be recycled. // That is to say, the right to use the memory pointed by p is returned to the system. // However, the value of p is still the original value (wild pointer), and the value of p points to the original memory free (p);} if (p! = NULL) {free (p );}