gpt4 book ai didi

c++ - 删除与删除[]

转载 作者:行者123 更新时间:2023-11-30 17:33:17 25 4
gpt4 key购买 nike

Possible Duplicate:
( POD )freeing memory : is delete[] equal to delete ?

当我学C++时,那已经是很久以前的事了。我被告知永远不要使用 delete但是delete[]执行delete[]在单个对象上将相当于 delete 。我知道不要太相信老师,我想知道,这是真的吗?

是否有理由调用 delete而不是delete[]

我扫描了SO中可能相关的问题,但没有找到任何明确的答案。

最佳答案

来自标准(5.3.5/2):

In the first alternative (delete object), the value of the operand of delete shall be a pointer to a non-array object or a pointer to a sub-object (1.8) representing a base class of such an object (clause 10). If not, the behavior is undefined.

In the second alternative (delete array), the value of the operand of delete shall be the pointer value which resulted from a previous array new-expression. If not, the behavior is undefined.

所以不:它们绝不等同!

关于c++ - 删除与删除[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23795550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com