问题描述
我理解,当我们在由相应的 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?
问一个相关的后续问题是最接近官方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.
这篇关于删除[]在调用析构函数后一次性释放内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!