When we use pointers, we often get the following kinds of errors:
1) The memory allocation was unsuccessful, but it was used.
Novice programmers often make this mistake because they are unaware that the memory allocation will not succeed. A common workaround is to check if the pointer is null before using memory. If the pointer p is an argument to a function, it is checked with an assert (P!=null) at the entrance of the function. If you are using malloc or new to request memory, you should use if (p==null) or if (P!=null) for error-proof handling.
2) The memory allocation succeeds, but it is not initialized to reference it.
There are two main causes of this error: one is the idea of no initialization, and the other is to mistakenly assume that the default initial value of the memory is all zero, resulting in a reference to the initial error (for example, an array). There is no uniform standard for what the default initial value of memory is, although sometimes it is a zero value and we would rather believe it to be credible. So no matter how to create an array, do not forget to assign the initial value, even if it is assigned 0 values can not be omitted, do not bother.
3) The memory allocation succeeds and has been initialized, but the operation crosses the memory boundary.
For example, the use of arrays often occurs when the subscript "more 1" or "less 1" operation. Especially in a For loop statement, the number of loops can be easily mistaken, resulting in array operations being out of bounds.
4) forgot to release memory, causing memory leak.
The function that contains this error loses one piece of memory each time it is called. At first, the system has plenty of memory and you can't see the error. One time the program suddenly died, the system appears prompt: memory exhaustion. Dynamic memory application and release must be paired, the program malloc and free use must be the same number, otherwise there must be errors (new/delete).
5) Release the memory and continue to use it. There are three situations: (1) The object call relationship in the program is too complex, it is difficult to know whether an object has freed the memory, at this time should redesign the data structure, fundamentally solve the chaos of object management. (2) The return statement of the function is incorrectly written, and be careful not to return a pointer or reference to "stack memory" because the memory is automatically destroyed at the end of the function body. (3) After releasing memory with free or delete, the pointer is not set to null. Causes the "wild pointer" to be produced.
There are a few principles that we should follow in the process of using pointers:
After rule 1 has requested memory with malloc or new, you should immediately check that the pointer value is NULL. Prevents the use of memory with a pointer value of NULL.
Rule 2 Do not forget to assign an initial value to both arrays and dynamic memory. Prevents memory that is not initialized from being used as the right value.
"Rule 3" avoids array or pointer subscript out of bounds, especially beware of "more 1" or "less 1" operations.
"Rule 4" the request and release of dynamic memory must be paired to prevent a memory leak.
"Rule 5" after releasing memory with free or delete, immediately set the pointer to NULL to prevent the "wild pointer" from being produced.
Let's look at an example of a wild pointer that might appear:
We are using the structure of the body loop nested structure pointer problem, error concealment. Take a look at the following code:
Define a struct, containing an int variable b
typedefstruct{ intb;} A
Defines a struct B, which consists of a struct pointer A and an int variable ctypedefstruct{A*A; intC;} B
Initialize b struct pointervoidInitialization (b*MyB) {MyB->c=8;}voidMainintargcChar*argv) {A MyA; MYA.B=8; B*MyB; Initalization (MyB); MyB->a->b=9; printf ("The values of c:%d", myb->c);}
Both Module 1 and Module 2 are normal struct declarations without any problems. Module 3 and Module 4, which involve pointer problems, require careful consideration, and the above code will report a segment error when it is run on the compiler.
WORKAROUND: We must initialize at the same time when declaring a pointer variable of type, and cannot turn it into a wild pointer. Both Module 3 and Module 4 are the same problem, the pointer variable itself requests the memory space, but its point to that piece of the type address is not defined, so we want to request space for it and initialize the pointer variable:
typedefstruct{ intb;} A;typedefstruct{A*A; intC;} B;voidInitialization (b*MyB) {MyB->a =malloc(sizeof(A));/* Do not forget to initialize */MyB->c=8;}voidMainintargcChar*argv) {A MyA; MYA.B=8; B* MyB =malloc(sizeof(B);/* Initialize/initalization (MyB) prior to use; MyB->a->b=9; printf ("The values of c:%d", myb->c);}
This will not cause a segment error, and you will find that the code is working correctly. Finally, don't forget to use the free (MyB).... Free yourself from the memory allocated in the heap and develop a good habit.
Summary: The pointer variable is used to store the memory address, and itself is a variable, but also to allocate the address in memory, and programmers tend to store the address itself as a storage of other types of address, so that when access to its pointer variable is not stored address, Access violation, a segment error occurred. When we declare a pointer variable, be sure to do so as long as it is used, and initialize it when it is declared.
The C + + pointers