删除[]在调用析构函数后一次性释放内存? [英] Does delete[] deallocate memory in one shot after invoking destructors?

查看:173
本文介绍了删除[]在调用析构函数后一次性释放内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解,当我们在由相应的 new [] 创建的指针上写入 delete [] 程序将查找数组上的会计信息,并找出数组的元素大小(计数器)。然后程序调用每个元素的析构函数。最后,通过名为 operator delete 的函数释放内存(什么内存??)。

I understand that, when we write delete [] on a pointer created by a corresponding new [], the program will look for the accounting information on the array and find out the array's element size (a counter). Then the program invokes the element's destructor on each of them. Finally memory (what memory??) is deallocated by a function named operator delete.

是否 delete [] 将在一次性释放中释放由 new [] 表达式分配的整个内存,因为该信息(内存总量)在之后所有元素都可用,或者连续取消分配其所拥有的数组元素所占用的内存调用析构函数?

What I am asking is whether delete[] will deallocate the entire memory, allocated by the new[] expression, in one shot because that information (total amount of memory) is available after all elements are destroyed, or will it successively deallocate the memory occupied by the array elements that it has invoked destructors on?

问一个相关的后续问题 p>

A related follow-up quesion is asked Does delete (non array form) know the total amount of memory allocated by either new or new[]

推荐答案

所有内存将立即释放给底层分配器。这是由C ++标准强制,虽然不是特别清楚。 N3337 是最接近官方C ++ 11标准在线免费。看看第5.3.5节,[expr.delete],它说明了调用 delete [] 的代码。特别地,在步骤7中之后之后之后调用运算符delete []

All of the memory will be released to the underlying allocator at once. This is mandated by the C++ standard, although not especially clearly. N3337 is the closest approximation to the official C++11 standard available online for free. Look at section 5.3.5, [expr.delete], which spells out the code that an invocation of delete[] expands to. In particular, operator delete[] is called once in step 7, after destructors have been invoked for all elements of the array (step 6).

您还可以从18.6.1.2 [new.delete.array]中的措辞中推断出这个行为,指出哪些指针有效传递给 operator delete [] :...应该是之前调用 operator new [] ... caveats ...)。由于 operator new [] 返回一个指向整个数组的指针,因此 operator delete [] 整个数组。

You can also deduce this behavior from the wording in 18.6.1.2 [new.delete.array] regarding what pointers are valid to pass to operator delete[]: "... shall be the value returned by an earlier call to operator new[] (... caveats ...)". Since operator new[] returns one pointer to an entire array, operator delete[] must also be invoked once for an entire array.

这篇关于删除[]在调用析构函数后一次性释放内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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