delete []运算符的时间复杂度 [英] Time complexity of delete[] operator

查看:304
本文介绍了delete []运算符的时间复杂度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

delete [] 运算子的时间复杂性是什么?

我的意思是它是如何实现的 - 它是否遍历数组中的所有元素,并为每个元素调用析构函数?

I mean how is it implemented - does it iterate over all the elements in the array and calls destructor for every element?

此运算符对原始类型 int 等)和用户定义的类型是否一样?

Does this operator do the same for primitive types (int, etc.) and user defined types?

推荐答案

:: operator delete [] 记录在 cplusplus.com (其中有时会皱眉):

::operator delete[] is documented on cplusplus.com (which is sometimes frowned upon) as:


操作符delete [] 作为常规函数,但在C ++中, delete [] 是具有非常特定行为的运算符:具有 delete [] 运算符,首先为数组中的每个元素调用适当的析构函数(如果这些是类类型)调用函数 operator delete [] (即,此函数)释放存储。

operator delete[] can be called explicitly as a regular function, but in C++, delete[] is an operator with a very specific behavior: An expression with the delete[] operator, first calls the appropriate destructors for each element in the array (if these are of a class type), and then calls function operator delete[] (i.e., this function) to release the storage.

所以析构函数被称为 n 次(每个元素一次),然后释放内存的函数被调用一次。

so the destructor is called n times (once for each element), and then the memory freeing "function" is called once.

请注意,每个销毁可能需要与其他销毁不同的时间(甚至复杂性)。通常大多数破坏是快速的,并且具有相同的复杂性....但是,如果每个被破坏的元素是一个复杂的树或节点或图形...

对于 int 的原始类型, int 的虚构析构函数是无操作。

For primitive types like int the fictitious destructor of int is a no-op. The compiler probably would optimize that (if asked).

您应该检查真实的 C ++ 11 标准,或至少其晚期 n3337 工作草案,其中(感谢 Matteo Italia 在评论中指出) )在n3337的§5.3.5.6第110页中:

You should check the real C++11 standard, or at least its late n3337 working draft, which says (thanks to Matteo Italia for pointing that in a comment) in §5.3.5.6 page 110 of n3337:


如果 delete-expression 不是空指针值,则
将调用对象或要删除的数组元素的析构函数(如果有的话)。
数组的情况下,元素将按照地址递减的顺序被销毁(即,按照它们的构造函数的完成
的相反顺序;见12.6.2)

If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted. In the case of an array, the elements will be destroyed in order of decreasing address (that is, in reverse order of the completion of their constructor; see 12.6.2)

如果您使用和足够信任 - GCC 4.8 < a>或更好,您可以使用 g ++ 编译器与 -fdump-tree-phiopt -fdump-tree-all 选项(要小心,它们会转储大量文件!)或 MELT 插件,查询中间Gimple表示的一些例子。或者使用 -S -fverbose-asm 获取汇编代码。您还需要添加优化标志,例如 -O1 -O2 ...

If you use -and trust enough- GCC 4.8 or better, you could have used the g++ compiler with the -fdump-tree-phiopt or -fdump-tree-all option (beware, they are dumping a lot of files!), or the MELT plugin, to query the intermediate Gimple representation of some example. Or use -S -fverbose-asm to get the assembler code. And you also want to add optimization flags like -O1 or -O2 ...

NB:IMHO, cppreference.com 也是一个关于C ++的有趣网站,请参阅在这里关于 delete (如注释由 Cubbi

NB: IMHO, cppreference.com is also an interesting site about C++, see there about delete (as commented by Cubbi)

这篇关于delete []运算符的时间复杂度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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