NULL pointer and Its Solution

Source: Internet
Author: User

/* What is an empty hanging pointer: the lifetime and end of the storage area to which the pointer points, but the lifetime of the pointer has not ended, resulting in
The data in the bucket has been released, and the pointer refers to a random value in the region. This pointer is called an empty pointer.
There are two common situations of the NULL pointer:
1. The function returns the address of an automatic local variable. I used to see this often.
2. After deleting a dynamically allocated object, the pointer is not 0. When this pointer is used later, it actually points to
Random value.
Note that the lifetime of the pointer is different from that of the region indicated by the pointer during dynamic allocation.
The lifetime of the pointer starts from the beginning of the program and ends from the end of the program. The storage interval of the pointer itself is compile time.
OK. the lifetime of the dynamically allocated range indicated by the pointer starts from new to the end of delete.
If the pointer is not pointed to 0, an empty pointer is displayed, which is easy to make mistakes.

Solution: 1. Corresponding to Case 1, the function returns a static local variable.
2. Corresponding to case 2. After delete is called, the pointer is null.
*/
# Include <iostream>
Using namespace std;
Int * add1 (const int & a, const int & B)
{
Int c = a + B;
Cout <"the internal result of the Add1 function is" <C <Endl;
Return & C;
}
// Haha, when compiling this function, vc6.0 provides the following warning:
// Warning c00002: returning address of local variable or temporary

Int * Add2 (const Int & A, const Int & B)
{
Static int c = A + B;
Cout <"the internal result of the Add2 function is" <C <Endl;
Return & C;
}

Int * add3 (const Int & A, const Int & B)
{
Int * P = new int;
* P = A + B;
Cout <"the internal result of the add3 function is" <* P <Endl;
Return P;
}

Void main ()
{
Int A = 1, B = 2, * P;
Cout <"Call Add1 to return an empty pointer/N ";
P = Add1 (A, B );
Cout <"pointer refers to" <* P <Endl;
Cout <"does the value indicated by the pointer change? /N "<(* P = 3? "Not changed": "changed") <Endl;
Cout <"calling Add2 can avoid returning an empty hanging Pointer" <Endl;
P = Add2 (A, B );
Cout <"pointer refers to" <* P <Endl;
Cout <"does the value indicated by the pointer change? /N "<(* P = 3? "Not changed": "changed") <Endl;
Cout <"calling add3 can avoid returning an empty hanging Pointer" <Endl;
P = add3 (A, B );
Cout <"pointer refers to" <* P <Endl;
Cout <"does the value indicated by the pointer change? /N "<(* P = 3? "Not changed": "changed") <Endl;
Delete P;
P = 0;
}

 

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.