2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
We know that delete is used to release a dynamic space, and delete[] is used to release multiple dynamic spaces, but what problems will it cause if we mix them? I have read many articles on blogs, but they are not satisfactory, so I wrote this article to let you know the whys and wherefores (Shallow explanation, hope you can correct me)。
---------------------------------------------------------------------------------------------------------------------------------
Let's look at an example first:
The running results are:
But if we don't write the destructor explicitly:
The result is normal operation:
---------------------------------------------------------------------------------------------------------------------------------
why?
If I ask you how much space the system allocates for these 10 A objects, you will probably answer 40 bytes. It is easy to understand. One A is 4 bytes, and 10 is 40 bytes.But in fact, if you write your own destructor, then a total of 44 bytes are allocated, and if you don't write a destructor, then a total of 40 bytes are allocated.。
Why does writing a destructor add 4 bytes? We know that the destructor will be automatically called after the space is released using delete (http://t.csdnimg.cn/f2FOj), (1) At this time, if we write the destructor ourselves, delete needs to call it (In fact, delete is also a function, which is equivalent to a nested function call.), then the number of calls is a problem. These four bytes are exactly the size of an integer, which is used to record the number of objects in this space, thereby determining how many times the constructor and destructor need to be called.However, you should know that the p pointer still points to the first object, but it will go forward to get the number of objects during construction and destruction., the specific relationship is as follows:
(2) If we do not write a destructor at this time, it will call the destructor generated by the compiler, and there is no need to record the number of times (it may involve a lower level, I am not sure, please correct me), and there is no need to open an extra integer.
OK, now we can explain the above example.
By default, delete releases the space from the location pointed to by the pointer; while delete[ ] by default checks whether there is extra space first. If there is extra space, the pointer moves forward one position before releasing it. If there is extra space and delete is used to release the space, the extra space cannot be released, resulting in a memory leak, so an error is reported.
Will there be an error if I practice this? :
There is no need to call the destructor, so there is no need to count the number of objects, and no extra space is needed. Therefore, the space can be released from the location pointed to by the pointer, so no error will be reported.
There are many situations, you can go down and try more by yourself.