[Reprinted] The delete pointer should be assigned a value of NULL and deletenull.

Source: Internet
Author: User

[Reprinted] The delete pointer should be assigned a value of NULL and deletenull.

First, the C ++ standard stipulates that the delete NULL pointer is legal and has no side effects.
However, after the delete p operation, only the memory space pointed to by the pointer is released. P is not automatically set to NULL, And the pointer is still pointing to the previous address.

The problem arises. After a non-NULL pointer is deleted, if NULL is not assigned and the delete operation is performed again, the problem may occur.
The following code

int *p = new int(3);delete p;delete p;

  


Problems may occur when compiled and run with VC.
Change it:

int *p = new int(3);delete p;p = NULL;delete p;

  


No problem occurs (because the delete NULL pointer is legal)
Therefore, to avoid problems, the pointer should be assigned NULL after being deleted.


In C ++, if the pointer is deleted without being assigned NULL, what kind of state is the pointer. Does it have memory space to point?

The pointer value remains unchanged, that is, it also points to the space you applied
However.
Now that the delete operation is complete, the space will no longer be applied to your program, so even though your pointer points to the memory space
However, you do not have the right to use or access the space.
So if you try to use it after the delete operation, the program will crash.
So it is best to assign NULL to prevent errors caused by accidental reference during program writing.

C language. After a pointer is deleted, the value is NULL and the value is deleted again. This will cause an error.

First, the C language does not include the delete C language, and some of the C language is free. The result of a null value is undefined.
Therefore, the result is that the compiler does what it wants and you don't know what happened.

According to the current C ++ Draft, Working Draft, Standard for Programming Language C ++
Working Draft, Standard for Programming Language C ++ wrote
5.3.5
(6): If the value of the operand of the delete-expression is not a null
Pointer value, the delete-expression will invoke the destructor (if
Any) for the object or the elements of the array being deleted. In
Case of an array, the elements will be destroyed in order of decreasing
Address (that is, in reverse order of the completion of their
Constructor; see 12.6.2 ).

Call the Destructor if it is not a null pointer. In other words, delete NULL; nothing happens.

Related Article

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.