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 !
这篇关于删除与删除[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:删除与删除[]
基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01