删除(非数组形式)知道由新的或新的[]分配的内存总量。 [英] Does delete (non array form) know the total amount of memory allocated by either new or new[]

查看:161
本文介绍了删除(非数组形式)知道由新的或新的[]分配的内存总量。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题作为删除的一部分提出

This question was asked as part of Does delete[] deallocate memory in one shot after invoking destructors? but moved out as a separate question.

似乎(如果错误,请纠正我

It seems (Correct me if wrong) that the only difference between delete and delete[] is that delete[] will get the array size information and invoke destructors on all of them, while delete will destruct the only first one. In particular, delete also has access to the info on how much total memory is allocated by new[].

如果不关心破坏动态分配的数组元素,只关心 new new [] 被解除分配, delete 似乎能够做同样的工作。

If one doesn't care about destructing the dynamically allocated array elements, and only care that the memory allocated either by new or new[] be deallocated, delete seems to be able to do the same job.

如何删除[]知道操作数数组的大小?问题的接受答案有一个来自@ AnT 的评论,我引用

This How does delete[] "know" the size of the operand array? question's accepted answer has one comment from @AnT and I quote


另请注意,数组元素计数器只适用于具有非平凡析构函数的类型。对于具有简单析构函数的类型,计数器不是由新的[]存储的,当然不是通过delete []检索的。

Also note that the array element counter is only needed for types with non-trivial destructor. For types with trivial destructor the counter is not stored by new[] and, of course, not retrieved by delete[]

建议一般来说 delete 表达式知道所分配的整个内存的数量,因此知道在一次中重新分配多少内存,甚至如果内存持有一个元素的数组。所以如果有人写

This comment suggests that in general delete expression knows the amount of the entire memory allocated and therefore knows how much memory to deallocate in one shot in the end, even if the memory hold an array of elements. So if one writes

auto pi = new int[10];
...
delete pi;

即使标准认为这是UB,在大多数实现上,这不应该泄漏内存是不可移植的),对吗?

Even though the standard deems this as UB, on most implementations, this should not leak memory (albeit it is not portable), right?

推荐答案

delete和delete []之间的区别是,后者知道在数组中分配的项目数,并对它们上的每个对象调用析构函数。要100%正确,实际上知道 - 分配给数组的项目数量等于分配的内存大小(两者都知道)除以对象的大小。

This is correct. Difference between delete and delete[] is that the latter knows the number of items allocated in the array and calls destructor on every object on them. To be 100% correct, both actually 'know' it - the number of items allocated for an array is equal to the allocated memory size (which both know) divided by the size of the object.

有人可能会问,为什么我们需要delete []和delete - 为什么不能删除执行相同的计算?答案是多态性。当通过指向基类的指针进行删除时,分配的内存的大小将不等于静态对象的大小。

One might ask, why do we need delete[] and delete than - why can't delete perform the same calculations? The answer is polymorphism. The size of the allocated memory will not be equal to the sizeof static objec when deletion is done through the pointer to the base class.

另一方面,delete []没有考虑对象被多角化的可能性,这就是为什么动态数组不应被视为多态对象分配和存储为指向基类的指针)。

On the other hand, delete[] does not take into account a possibility of object being polymorphed, and this is why dynamic arrays should never be treated as polymorphic objects (i.e. allocated and stored as a pointer to the base class).

对于泄漏的内存,在数组上使用POD类型时,delete不会泄漏内存。

As for leaking memory, delete will not leak memory in case of POD types when used on arrays.

这篇关于删除(非数组形式)知道由新的或新的[]分配的内存总量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆