The "wild pointer" is not a null pointer, but a pointer to the "junk" memory. Generally, null pointers are not incorrectly used, because if statements are easy to judge. However, the "wild Pointer" is very dangerous, and the IF statement does not work for it.
There are two main causes of "wild pointer:
(1) pointer variables are not initialized. When a pointer variable is created, it does not automatically become a null pointer. Its default value is random, which means it is random. Therefore, the pointer variable should be initialized at the same time when it is created, either set the pointer to null or set it to direct to the legal memory. For example
Char * P = NULL;
Char * STR = (char *) malloc (100 );
(2) After the pointer P is free or deleted, It is not set to null, which makes people mistakenly think P is a valid pointer.
Refer to free and delete what's wrong with pointers?
(3) pointer operations go beyond the scope of variables. This situation is hard to prevent. The example program is as follows:
Class
{
Public:
Void func (void) {cout <"func of Class A" <Endl ;}
};
Void test (void)
{
A * P;
{
A;
P = & A; // note the life cycle of
}
P-> func (); // P is a "wild pointer"
}
When the function test executes the statement p-> func (), object A has disappeared, and P points to a, so P becomes a "wild pointer ". But the strange thing is that I did not encounter any errors when running this program, which may be related to the compiler.