C ++ new Delete

Source: Internet
Author: User

1. When you use new, two things will happen. First, the memory is configured (through the operator new function ). Second, one or more constructors will be called for this memory. When you use Delete, there are also two things: one or more Destructors will be called for this memory, and then the memory will be released (through the operator delete function ).

2. If you use delete without parentheses, delete assumes that the deleted object is a single object. Otherwise, it is assumed that the object to be deleted is an array.

3. String * stringptr1 = new string;

String * stringptr2 = new string [100];

......

Delete stringptr1;

Delete [] stringptr2;

If you use the [] form to stringptr1, the result is undefined. If you do not use the [] format for stringptr2, the result is also undefined. The inner type such as Int Is also undefined, even if this type does not have Destructors.

4. therefore, the game rules are very simple. If you use [] when calling New, you also use [] when calling Delete. If you do not call New when [], then you should not use [] During the call.


 

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.