什么时候在C ++中使用new关键字? [英] When should I use the new keyword in C++?
问题描述
我一直在使用C ++,我一直在想知道新关键字。
1)使用新关键字...
MyClass * myClass = new MyClass();
myClass-> MyField =Hello world!;
2)没有新关键字...
MyClass myClass;
myClass.MyField =Hello world!;
从实现的角度来看,他们似乎没有什么不同...但是,我的主要语言是C#,当然第一个方法是我习惯了。
困难似乎是方法1更难
更新1:
使用std C ++类我最近使用堆内存的新关键字(或免费存储数组超出范围(即从函数返回)。在我使用堆栈之前,这导致一半的元素在范围之外被破坏,切换到堆使用确保元素在生产中。 Yay!
更新2:
我最近的朋友告诉我有一个简单的规则, code> new 关键字;每次输入 new
时,键入 delete
。
Foobar * foobar = new Foobar();
delete foobar; // TODO:将它移动到正确的位置。
这有助于防止内存泄漏,因为你总是把删除某处
方法1(使用 > 方法2(不使用新建的方法) www.gotw.ca/gotw/009.htm\">free store (这通常与堆相同)
)
delete
您的对象。
delete
为止。 (即您可以 return
使用 new
)创建的对象
delete
d;
新
$
- 为堆栈(所有本地变量都去)的对象分配内存通常,堆栈可用的内存较少;
- 以后不需要
删除
。 - 当内存超出范围时,不再分配内存。 (即,您不应
返回
指向堆栈上的对象的指针)
至于使用哪一个;考虑到上述约束,您选择最适合您的方法。
一些简单情况:
- 不要担心调用
delete
(以及可能导致内存泄漏),你不应该使用new
。 - 如果你想返回一个指针对象,必须使用
new
I've been using C++ for a short while, and I've been wondering about the new keyword. Simply, should I be using it, or not?
1) With the new keyword...
MyClass* myClass = new MyClass();
myClass->MyField = "Hello world!";
2) Without the new keyword...
MyClass myClass;
myClass.MyField = "Hello world!";
From an implementation perspective, they don't seem that different (but I'm sure they are)... However, my primary language is C#, and of course the 1st method is what I'm used to.
The difficulty seems to be that method 1 is harder to use with the std C++ classes.
Which method should I use?
Update 1:
I recently used the new keyword for heap memory (or free store) for a large array which was going out of scope (i.e. being returned from a function). Where before I was using the stack, which caused half of the elements to be corrupt outside of scope, switching to heap usage ensured that the elements were in tact. Yay!
Update 2:
A friend of mine recently told me there's a simple rule for using the new
keyword; every time you type new
, type delete
.
Foobar *foobar = new Foobar();
delete foobar; // TODO: Move this to the right place.
This helps to prevent memory leaks, as you always have to put the delete somewhere (i.e. when you cut and paste it to either a destructor or otherwise).
Method 1 (using new
)
- Allocates memory for the object on the free store (This is frequently the same thing as the heap)
- Requires you to explicitly
delete
your object later. (If you don't delete it, you could create a memory leak) - Memory stays allocated until you
delete
it. (i.e. you couldreturn
an object that you created usingnew
) - The example in the question will leak memory unless the pointer is
delete
d; and it should always be deleted, regardless of which control path is taken, or if exceptions are thrown.
Method 2 (not using new
)
- Allocates memory for the object on the stack (where all local variables go) There is generally less memory available for the stack; if you allocate too many objects, you risk stack overflow.
- You won't need to
delete
it later. - Memory is no longer allocated when it goes out of scope. (i.e. you shouldn't
return
a pointer to an object on the stack)
As far as which one to use; you choose the method that works best for you, given the above constraints.
Some easy cases:
- If you don't want to worry about calling
delete
, (and the potential to cause memory leaks) you shouldn't usenew
. - If you'd like to return a pointer to your object from a function, you must use
new
这篇关于什么时候在C ++中使用new关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!