Delete and delete in C ++ []

Source: Internet
Author: User

This articleArticleThis is an issue that has been discussed many times in the csdn C ++ forum.

Let's take a look at the following:Program:

 # Include <iostream> 
Using NamespaceSTD;
# Include <String>

IntMain ()
{
Int* P =New Int[5];
//Delete P;
Delete [] P;
P = NULL;

//String * P = new string [5];
//Delete P;
//Delete [] P;
//P = NULL;

Return 0;
}

For int and string types, the Delete P and delete [] P compilers (vc6.0) have different results. For int type Delete P and delete [] P, both can be compiled and run, for the string type, delete p runs

The program will crash.

The following are my comments on this issue and my summary:

(1) Delete and delete [] release all the memory. The difference is only that, based on the obtained type information, whether it is a single pointer or a pointer to an array, to determine the number of times the Destructor is called. The compiler determines whether the pointer to be deleted is a single object or an array of Objects Based on. If the type information obtained by the '[]' compiler is the pointer to the array, and then the Destructor is called multiple times, if the type information obtained by the '[]' compiler is a single pointer, only one destructor is called.
In fact, the delete operation itself is to do two things:
<1> calls destructor for memory
<2> then release the memory.
(For details, see Objective C ++ (2nd) Item 5 or (3rd) item 16 ).

 

(2) In vc6.0 and vs05, the delete and delete [] Statements call operator Delete (), debug in vs05, and follow up operator Delete, we can see that there is such a sentence "_ free_dbg (puserdata, phead-> nblockuse);" and free should work correctly, there is a necessary premise, that is, the address passed to it is indeed the first address of the memory originally applied for. Otherwise, an Assert error will occur. After the delete operator is overloaded, it is found that the pointer address of the delete operation is different from the address obtained by the new operation. As a result, the assert error occurs and the program crashes.

(3) In VC, for an object with an explicit destructor, a 4-byte prefix before the array is allocated to save the number of array elements. if delete is used to release the array, the released memory address may be 4-byte different from the allocated memory address, resulting in a catastrophic error.

(4) under what circumstances does the pointer address of the two operations do not match?
After repeated tests, we found that as long as the object type definition has an explicit destructor, this 4-byte exists. That is to say, even if we have a custom class type object, as long as we have not defined the Destructor for this class, this 4-byte problem will not occur; when Delete [] is used, no error will be reported if Delete is used. If Delete [] is used, no error will be reported...
This topic is a coincidence that the built-in types have no destructor, while the string has the destructor, the problem is limited to the classification scope of built-in and custom types. Therefore, to be accurate, the existence of explicit destructor affects the processing of Delete and delete.

 

(5) One sentence: Delete [] is not omitted. This is the c ++ standard.

 

The original post is as follows:

(1) http://topic.csdn.net/u/20080826/16/0c479e3d-737f-45c7-995e-bd316f5fa166.html

(2) http://topic.csdn.net/u/20080221/20/7c7093b9-62d7-4d70-b959-3a06e4327d46.html

(3) http://topic.csdn.net/u/20070712/07/57c7cfc6-7314-400d-86d2-230a72581ea5.html

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.