About Delete []

Source: Internet
Author: User

Generally, when deleting [] an array, the compiler should do the following two tasks in order:
1. Call the Destructor for each array element
2. Release the memory occupied by the object Array
Note that this is two independent steps.

In step 2. the compiler simply tells the operating system the first address of the array. The operating system records the memory application status (the first address and length of each applied memory segment, Etc ..), therefore, the memory will be correctly released. note that the memory occupied by the entire array is released once instead of once for each element. in fact, in this step, the compiler does not need to know the number of array elements.

Of course, you do need to know the number of elements in step 1. Therefore, as the author said, the compiler will put the number of elements in front of the allocated memory. The structure is as follows:
N object1 object2... objectn
Do not confuse N with the Array Memory Length. the former is managed by the compiler, and the latter is managed by the operating system. (If you know the object size, you can calculate the former from the latter. unfortunately, the operating system does not have the length of the memory segment requested by the API, so the compiler has to record it on its own)

Note that if the array element does not have an explicit Constructor (such as char), the compiler does not need to perform step 2. in this case, the compiler does not need to know the number of arrays at all, so it will be lazy and no longer put the number of elements N in front of the 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.