Experience in using c ++ pointers

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.