新与新[1] [英] new vs new[1]

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

问题描述

您好,


为什么我不能写新[1]而不是新

申请中到处都是?所以,我不需要跟踪我必须在哪里调用删除和

其中delete []。因此,它将简化编码。


或者应用程序性能将大幅降低?


谢谢,

Denis 。

Hello,

Why do i can''t write "new [1]" instead of "new" everywhere in
applications? So, i will not need to track where i must call delete and
where delete[]. Therefore, it will simplify coding.

Or application performance will be significantly reduced?

Thanks,
Denis.

推荐答案

" Denis Petronenko" < pe ******** @ gmail.comschrieb im Newsbeitrag

新闻:11 ********************** @ 75g2000cwc.googlegro ups.com ...
"Denis Petronenko" <pe********@gmail.comschrieb im Newsbeitrag
news:11**********************@75g2000cwc.googlegro ups.com...

您好,


为什么我不能写新[1]而不是新

申请中到处都是?所以,我不需要跟踪我必须在哪里调用删除和

其中delete []。因此,它将简化编码。


或者应用程序性能会大幅降低?
Hello,

Why do i can''t write "new [1]" instead of "new" everywhere in
applications? So, i will not need to track where i must call delete and
where delete[]. Therefore, it will simplify coding.

Or application performance will be significantly reduced?



你可以,但是你也可以使用这种

对象的默认构造函数。你必须更换简单的代码,比如


std :: string * pstr = new std :: string(" Some string");


类似


std :: string * pstr = new std :: string;

* pstr =" some string"; <
这可能确实会降低性能。另外,如果你知道你在做什么,并且你的变量名称已经选好,你应该很容易知道何时使用删除以及何时使用delete [] 。


如果太难以处理,不要在

全部使用new []。使用std :: vector代替C样式数组。


你还应该考虑使用RAII方法来处理内存分配。

为指针编写一个包装类单个对象(或使用std :: auto_ptr)

和一个类似于数组指针的包装器。一旦你习惯了,你就不用担心删除了。


HTH

亨氏

You can, but then you could also use the default constructor of such
objects. You''d have to replace simple code like

std::string* pstr = new std::string("Some string");

by something like

std::string* pstr = new std::string;
*pstr = "Some string";

and that might indeed reduce performance. Also, if you kow what you are
doing and the names of your variables are well chosen, you should easily
know when to use delete and when to use delete[].

And just in case that would be too difficult to handle, don''t use new[] at
all. Use std::vector instead a C style array.

You should also consider using a RAII approach to handle memory allocation.
Write a wrapper class for pointers to single objects (or use std::auto_ptr)
and a similiar wrapper for pointers to arrays. Once you get used to it, you
don''t have to worry about delete at all.

HTH
Heinz


文章< 11 ********************** @ 75g2000cwc.googlegroups 。 com>,

" Denis Petronenko" < pe ******** @ gmail.comwrote:
In article <11**********************@75g2000cwc.googlegroups. com>,
"Denis Petronenko" <pe********@gmail.comwrote:

为什么我不能写new [1]而不是新

申请中到处都是?所以,我不需要跟踪我必须在哪里调用删除和

其中delete []。因此,它将简化编码。
Why do i can''t write "new [1]" instead of "new" everywhere in
applications? So, i will not need to track where i must call delete and
where delete[]. Therefore, it will simplify coding.



如果您只调用''new''而不是

''new []'',它会简化编码,并使用std :: vector,无论你以前用过''new []''

It would simplify coding more if you only called ''new'' instead of
''new[]'', and used std::vector wherever you used to use ''new[]''


或者应用程序性能会大幅降低?
Or application performance will be significantly reduced?



我对此表示怀疑。

I doubt it.




Denis Petronenko写道:

Denis Petronenko wrote:

为什么我不能写new [1]而不是新

申请中到处都是?
Why can''t I write "new [1]" instead of "new" everywhere in
applications?



你可以。试试吧。

You can. Try it.


所以,我不需要跟踪我必须在哪里调用delete和

其中delete []。因此,它将简化编码。


或者应用程序性能会大幅降低?
So, i will not need to track where i must call delete and
where delete[]. Therefore, it will simplify coding.

Or application performance will be significantly reduced?



不显着。但是,new [1]将分配额外的空间来保存数字1的
。保存4个字节的情况并不少见。另外,删除[]

会慢一些,因为它使用一个循环来调用dtor一次。


区别的原因是这些是低层建筑

街区,不适合直接使用。有了聪明的指针,你根本不需要打电话给删除。 (和std :: vector你不能打电话

删除[])


HTH,

Michiel Salters。

Not significantly. However, new[1] will allocate extra space to save
the number 1. It''s not uncommon to save 4 bytes. Also, delete[]
will be a bit slower because it uses a loop to call the dtor once.

The reason for the distinction is that these are low-level building
blocks, not intended for direct use. With smart pointers, you don''t
need to call delete at all. (and with std::vector you don''t call
delete[])

HTH,
Michiel Salters.


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

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