C ++ delete a pointer using delete (2)

Source: Internet
Author: User

From the invigilation window, we can see that although the pointer p has been deleted in line 1 of the program, p still exists in the invigilation window, but * p points to a random number instead of 3. Here we illustrate a very important concept: After deleting a pointer, the compiler will only release the memory space pointed to by the pointer, rather than Delete the pointer itself.
Next, let's analyze it. In line 2 of the program, we created a long pointer p1. In the output of row 12 and row 13, we were surprised to find that the address saved by pointer p is exactly the same as that saved by pointer p1! This indicates that both pointer p and pointer p1 point to the same place in the memory !!! This is because of the compiler. By default, the compiler recycles the released memory space and allocates it to the new space. Since we have opened a new space for storing long variables in row 11th and point to it by p1, in this case, p1 actually points to the memory space released in Row 3 of the program, that is, the memory space directed by p! Therefore, the two pointers point to the same memory space at the same time. This is an insecure thing! You know, we deleted the pointer p! If * p is assigned again, will it be changed together with * p1?

Indeed, what worries us has emerged. We clearly defined * p1 as 11th in line 1 of the program, but in the output, the value read by the pointer p1 is also 23. This is caused by the wild pointer p. We can see that in the second line of the program, we assigned 23 to * p. Since p and p1 point to the same memory unit, here it is equivalent to changing the value of the memory unit (originally 100) pointed to by p1 to 23! This will inevitably lead to program errors!

So we can't help but ask, are there any solutions to this problem caused by the wild pointer? The answer is yes, of course. We only need to remember the following sentence:

After deleting a pointer, you must set it to a NULL pointer (that is, after delete * p, you must add: p = NULL)

Let's take a look at the definition of the keyword NULL in stdio. h:

1
2
 
3
# Ifndef NULL
4
# Ifdef _ cplusplus
5
# Define NULL 0
6
# Else
7
# Define NULL (void *) 0)
8
# Endif
9
# Endif
Note the 5th rows defined above. In fact, NULL is 0. That is to say, after deleting the pointer p, we must turn it into a null pointer! Only in this way will the errors of the wild pointer in the above program be eliminated.

P.s. For NULL applications, we should not be limited to the above method, but can also use NULL to determine whether the pointer Initialization is successful, as shown in the following example:

 

01
# Include
02
Using namespace std;
03
 
04
Int main ()
05
{
06
Int * p = new int;
07
If (p = NULL)
08
{
09
// Judge whether the pointer p is a null pointer. If it is a null pointer, the program should report an error here.
10
// There are many ERROR reporting methods, such as returning an ERROR value:
11
// Return ERROR;
12
}
13
 
14
// A series of operations can be performed only after the operation is successful
15
//...
16
 
17
// After the pointer p is used up, delete it. This prevents the existence of the wild pointer.
18
Delete p;
19
// After deleting the pointer p, add the following sentence to avoid being a wild pointer.
20
P = NULL;
21
}

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.