删除与删除[] [英] delete vs delete[]

查看:22
本文介绍了删除与删除[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
(POD)释放内存:delete[]等于delete吗?

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

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

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

来自标准 (5.3.5/2) :

<块引用>

在第一个选项中(删除对象),操作数的值delete 应该是一个指向 a 的指针非数组对象或指向 a 的指针代表基础的子对象 (1.8)此类对象的类(第 10 条).如果不是,则行为未定义.

在第二种选择中(删除数组),操作数的值delete 应该是指针值这是由先前的数组产生的新表达.如果没有,则行为未定义.

所以不:它们绝不是等价的!

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 !

这篇关于删除与删除[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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