If the variable address of scope A is assigned to the variable pointer of scope B. When the lifecycle of the corresponding variable in A ends, the pointer of B will appear as A "suspension Pointer" (the suspension Pointer Points to the memory of the previously stored object, but the object no longer exists. Hanging pointers often lead to program errors and are difficult to detect .) The following code:
# Include <iostream>
# Include <windows. h>
Using namespace std;
Int * p = NULL;
Void fun ()
{
Int I = 10;
P = & I;
}
Void main ()
{
Fun ();
Cout <"* p =" <* p <endl;
Sleep (1000 );
Cout <"one second later, the storage space of the I variable in fun () is released, and the value of p indicates the object is: "<endl <" * p = "<* p <endl;
}
Output:
* P = 10
One second later, the storage space of the I variable in fun () is released, and the value of p indicates the object is:
* P = 1245056
Press any key to continue
It can be seen that after fun () runs for one second, p becomes the suspension pointer.