为什么C ++需要删除[]? [英] Why does C++ need delete[] ?

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

问题描述

我理解删除xp删除一个标量对象和delete [] xp

删除一个对象数组,但我不明白为什么你需要

告诉编译器你正在做什么。


当你执行删除[] xp时,删除程序(不确定是否是'

正确的术语)显然知道对应的新对象分配了多少个对象。呼叫。所以,为什么不能只知道你是否b $ b做了新x或new x [n]?

I understand that "delete xp" deletes a scalar object and "delete [] xp"
deletes an array of objects, but what I don''t understand is why you need
to tell the compiler which you''re doing.

When you do "delete [] xp", the delete procedure (not sure if that''s the
right terminology) obviously knows how many objects were allocated by
the corresponding "new" call. So, why can''t it just know whether you
did "new x" or "new x[n]"?

推荐答案

" Roy Smith" < ro*@panix.com>写道...
"Roy Smith" <ro*@panix.com> wrote...
我理解删除xp删除一个标量对象和delete [] xp
删除一个对象数组,但我不明白为什么你需要告诉编译器你在做什么。<当你执行删除[] xp时,删除程序(不确定这是否是正确的术语)显然知道
分配了多少个对象相应的新呼叫。所以,为什么不能只知道你是否做了新的x。或new x [n]?
I understand that "delete xp" deletes a scalar object and "delete [] xp"
deletes an array of objects, but what I don''t understand is why you need
to tell the compiler which you''re doing.

When you do "delete [] xp", the delete procedure (not sure if that''s the
right terminology) obviously knows how many objects were allocated by
the corresponding "new" call. So, why can''t it just know whether you
did "new x" or "new x[n]"?




删除的存在以及删除[]允许一些释放

代码要优化得更好一些。例如,如果您确定

只有一个对象需要销毁,则无需查询bn大小的数组,所以''删除''比它的

数组更高效。对比。


如果你在删除

一个大小为1的数组而不是单个对象时进行了性能测试,你会看到差异。


V



Existence of ''delete'' along with ''delete[]'' allows some deallocation
code to be optimised a bit better. For example, if you are sure that
there is only one object to be destroyed, there is no need to look up
the size of the "array", so ''delete'' is a bit more efficient than its
"array" counterpart.

You could see the difference if you did a performance test on deleting
an array of size 1 versus a single object.

V




" Victor Bazarov" <五******** @ comAcast.net>在消息中写道

news:XXTmd.110103

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:XXTmd.110103


R05.47638@attbi_s53 ...
R05.47638@attbi_s53...
" Roy Smith" < ro*@panix.com>写道...
"Roy Smith" <ro*@panix.com> wrote...
我理解删除xp删除一个标量对象和delete [] xp
删除一个对象数组,但我不明白为什么你需要告诉编译器你在做什么。<当你执行删除[] xp时,删除程序(不确定这是否是正确的术语)显然知道
分配了多少个对象相应的新呼叫。所以,为什么不能只知道你是否做了新的x。或new x [n]?
I understand that "delete xp" deletes a scalar object and "delete [] xp"
deletes an array of objects, but what I don''t understand is why you need
to tell the compiler which you''re doing.

When you do "delete [] xp", the delete procedure (not sure if that''s the
right terminology) obviously knows how many objects were allocated by
the corresponding "new" call. So, why can''t it just know whether you
did "new x" or "new x[n]"?



删除的存在以及删除[]允许优化一些解除分配的代码好一点。例如,如果您确定只有一个对象要被销毁,则无需查找数组的大小,因此删除有点比它的数组效率更高对应。



Existence of ''delete'' along with ''delete[]'' allows some deallocation
code to be optimised a bit better. For example, if you are sure that
there is only one object to be destroyed, there is no need to look up
the size of the "array", so ''delete'' is a bit more efficient than its
"array" counterpart.




通过使用delete [],我无法想象很多优化优势。

多少表格查找真的花费了数组大小?如果删除

[]是为了更有效率,为什么他们没有添加免费[] (沿着

还带有malloc [])功能?


我猜他们选择了删除[]使其成为语法的补语

" new x [n]"。



I can''t imagine much of an optimization advantage by using "delete []". How
much does one table lookup really cost for the array size? And if "delete
[]" was that much more efficient, why didn''t they add a "free []" (along
with "malloc[]") feature as well?

I guess they chose to have "delete[]" to make it a compliment in syntax to
"new x[n]".


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

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