delete vs delete[](删除与删除[])
问题描述
<块引用>可能的重复:
(POD)释放内存:delete[]等于delete吗?一个>
当我学习 C++ 时,那是很久以前的事了.我被告知永远不要使用 delete
但 delete[]
因为在单个对象上执行 delete[]
将等同于 delete代码>.知道不要太相信老师,我想知道这是真的吗?
是否有理由调用 delete
而不是 delete[]
?
我已经扫描了 SO 中可能相关的问题,但没有找到任何明确的答案.
来自标准 (5.3.5/2) :
<块引用>在第一个选项中(删除对象),操作数的值delete 应该是一个指向 a 的指针非数组对象或指向 a 的指针代表基础的子对象 (1.8)此类对象的类(第 10 条).如果不是,则行为未定义.
在第二种选择中(删除数组),操作数的值delete 应该是指针值这是由先前的数组产生的新表达.如果没有,则行为未定义.
所以不:它们绝不是等价的!p>
Possible Duplicate:
( POD )freeing memory : is delete[] equal to delete ?
When I was taught C++, this was a long time ago. I was told to never use delete
but delete[]
as performing delete[]
on a single object will be equivalent to delete
. Knowing not to trust teachers too much I wonder, Is this true?
Is there ever a reason to call delete
instead of delete[]
?
I've scanned the possibly related questions in SO, but haven't found any clear answer.
From the standard (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.
So no : they are in no way equivalent !
这篇关于删除与删除[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:删除与删除[]


基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 这个宏可以转换成函数吗? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01