Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why the number of elements does not need to be indicated in parentheses when calling delete []

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about why there is no need to specify the number of elements in parentheses when calling delete []. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

First of all, make a simple knowledge preparation for the students who do not understand the problem.

There is a simple rule in C++ that the memory applied for by calling new is freed with delete; the memory requested by calling new [] must be freed with delete [].

Consider the following class definitions.

Public: Test () {}; ~ Test () {};}

When we want to create a new object in the heap, we can use the following code:

‍ Test* pTest = new Test

We can use the following code when we want to release this object:

Delete pTest

When we want to create 10 objects in the heap, we can use the following code:

Test* pTests = new Test [10]

When we want to release these 10 objects, corresponding to new [], we must use delete [].

Delete [] pTest

The difference between the two is that the belt new [] and delete [] call constructors and destructors for each element.

The above is what every C++ grammar book will be reduced to. It's kind of knowledge preparation.

The next ten questions: why do you not need to indicate the number of elements in parentheses when calling delete []?

It is said that early C++ needed to specify the number of elements when calling delete [], but that would be very troublesome. So later improvements were made to apply for memory space to save the number of elements when implementing new []. It basically looks like this:

Struct array {size_t count_of_test

Test t [10]

}

When calling new [], first apply for a little more space to save the count_of_test when requesting memory through malloc (), and then return the address of the t space to the user. This address adds an offset to the array address obtained by malloc. Finally, the constructor for each element is called on this t-space.

The argument to call delete [] is actually the address of the t-space. First, the array address and count_of_test are obtained by subtracting the offset of this address parameter, and then the destructor is called count_of_test times on the t-space. Finally, the free () function is called with the array address as an argument.

In addition to calling the constructor / destructor correctly, you have to deal with the space where the number of elements is saved.

So it must be used in pairs.

This is why you don't need to specify the number of elements in parentheses when calling delete []. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report