Differences between Delete and delete []

Source: Internet
Author: User

In Delete [], [] indicates C ++, pointing to an array. If the length of the array is specified in [], the C ++ compilation system will ignore it, and treat it. However, if you forget to write [],ProgramA running error occurs.


Delete calls the Destructor only once, while Delete [] calls the destructor of each member. In more efficient C ++, there is a more detailed explanation: "When the delete operator is used for an array, it calls the Destructor for each array element, and then calls operatordelete to release the memory ." Supports Delete and new, and supports Delete [] and new []

Memtest * mtest1 = newmemtest [10];

Memtest * mtest2 = newmemtest;

Int * pint1 = newint [10];

Int * pint2 = newint;

Delete [] pint1; //-1-

Delete [] pint2; //-2-

Delete [] mtest1; //-3-

Delete [] mtest2; //-4-

An error is reported at-4.

This indicates that the delete and delete [] functions are the same for built-in simple data types. For custom complex data types, delete and delete [] cannot be mutually used. Delete [] delete an array and delete a pointer In short, use Delete to delete the memory allocated by New , delete the memory allocated with New [] with Delete [] . Delete [] calls the destructor of the array element. Internal data types do not have destructor, so there is no problem. If you do not use parentheses when using Delete, delete will think that it points to a single object. Otherwise, it will think that it points to an array.

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.