我已经使用c++有一段时间了,我一直在想这个新的关键字。简单地说,我是否应该使用它?

使用new关键字…

    MyClass* myClass = new MyClass();
    myClass->MyField = "Hello world!";

没有new关键字…

    MyClass myClass;
    myClass.MyField = "Hello world!";

从实现的角度来看,它们似乎并没有什么不同(但我确信它们确实不同)……然而,我的主要语言是c#,当然第一个方法是我所习惯的。

困难在于方法1很难与std c++类一起使用。

我应该用哪种方法?

更新1:

最近,我为一个超出作用域(即从函数返回)的大数组使用了用于堆内存(或自由存储)的new关键字。在我使用堆栈之前,会导致一半的元素在作用域外损坏,切换到堆使用可以确保元素完好无损。耶!

更新2:

我的一个朋友最近告诉我,使用new关键字有一个简单的规则;每次输入new,都输入delete。

    Foobar *foobar = new Foobar();
    delete foobar; // TODO: Move this to the right place.

这有助于防止内存泄漏,因为您总是必须将删除放在某个地方(即当您剪切并粘贴到析构函数或其他方法时)。


当前回答

我应该用哪种方法?

这几乎从来不是由您的输入首选项决定的,而是由上下文决定的。如果你需要将对象保存在几个堆栈中,或者它对于堆栈来说太重了,你可以将它分配到免费存储中。此外,由于您正在分配一个对象,您还负责释放内存。查找删除操作符。

为了减轻使用免费商店管理的负担,人们发明了像auto_ptr和unique_ptr这样的东西。我强烈建议你看看这些。它们甚至可能对你的打字问题有帮助;-)

其他回答

如果变量只在单个函数的上下文中使用,那么最好使用堆栈变量,即选项2。正如其他人所说,您不必管理堆栈变量的生命周期——它们是自动构造和销毁的。而且,相比之下,在堆上分配/释放变量的速度较慢。如果函数被足够频繁地调用,使用堆栈变量而不是堆变量,您将看到巨大的性能改进。

也就是说,在一些明显的实例中,堆栈变量是不够的。

If the stack variable has a large memory footprint, then you run the risk of overflowing the stack. By default, the stack size of each thread is 1 MB on Windows. It is unlikely that you'll create a stack variable that is 1 MB in size, but you have to keep in mind that stack utilization is cumulative. If your function calls a function which calls another function which calls another function which..., the stack variables in all of these functions take up space on the same stack. Recursive functions can run into this problem quickly, depending on how deep the recursion is. If this is a problem, you can increase the size of the stack (not recommended) or allocate the variable on the heap using the new operator (recommended).

另一种更可能的情况是,您的变量需要“生存”在函数的作用域之外。在这种情况下,可以在堆上分配变量,以便在任何给定函数的作用域之外都可以访问它。

c++核心指南R.11:避免显式使用new和delete。

自从这个问题的答案被写出来以来,情况已经发生了显著的变化。具体来说,c++已经发展成为一门语言,标准库现在更加丰富。为什么这很重要?因为两个因素的结合:

使用new和delete有潜在的危险:如果你没有在不再使用时删除所有已分配的内容,那么内存可能会泄漏;永远不要删除当前未分配的资源。 标准库现在提供了封装new和delete调用的智能指针,这样你就不必自己管理免费存储/堆的分配了。标准库和其他地方的其他容器也是如此。

正如链接的文档所示,这已经发展成为c++社区编写更好的c++代码的“核心准则”之一。当然,这条规则也有例外:有人需要编写那些使用new和delete的封装类;但那个人很少是你自己。

补充@DanielSchepler的有效答案:

方法一(使用new)

为自由存储区上的对象分配内存(这通常与堆相同) 要求稍后显式删除对象。(如果你不删除它,你可能会创建一个内存泄漏) 内存一直被分配,直到删除它。(也就是说,你可以返回一个你用new创建的对象) 问题中的例子会泄漏内存,除非删除指针;无论采用哪条控制路径,或者是否抛出异常,都应该始终删除它。

方法二(不使用new)

为堆栈上的对象分配内存(所有局部变量都在堆栈上)。如果分配的对象太多,就有堆栈溢出的风险。 你以后不需要删除它。 当内存超出作用域时,不再分配内存。(也就是说,你不应该返回指向栈上对象的指针)

至于用哪一个;考虑到上述约束条件,您可以选择最适合自己的方法。

一些简单的例子:

如果不想担心调用delete(以及可能导致内存泄漏),就不应该使用new。 如果你想从函数返回指向对象的指针,你必须使用new

如果你是用c++写的,你可能是为了性能而写的。使用new和free store要比使用堆栈慢得多(特别是在使用线程时),所以只在需要时使用它。

正如其他人所说,当你的对象需要存在于函数或对象作用域之外,对象非常大,或者当你在编译时不知道数组的大小时,你需要new。

另外,尽量避免使用delete。把你的新代码包装成智能指针。让智能指针为你调用删除。

There are some cases where a smart pointer isn't smart. Never store std::auto_ptr<> inside a STL container. It will delete the pointer too soon because of copy operations inside the container. Another case is when you have a really large STL container of pointers to objects. boost::shared_ptr<> will have a ton of speed overhead as it bumps the reference counts up and down. The better way to go in that case is to put the STL container into another object and give that object a destructor that will call delete on every pointer in the container.

简单的回答是:如果你是c++的初学者,你永远不应该使用new或delete。

相反,你应该使用智能指针,例如std::unique_ptr和std::make_unique(或者很少使用std::shared_ptr和std::make_shared)。这样,您就不必太担心内存泄漏了。即使您更高级,最佳实践通常是将使用new和delete的自定义方式封装到一个专门用于对象生命周期问题的小类(例如自定义智能指针)中。

Of course, behind the scenes, these smart pointers are still performing dynamic allocation and deallocation, so code using them would still have the associated runtime overhead. Other answers here have covered these issues, and how to make design decisions on when to use smart pointers versus just creating objects on the stack or incorporating them as direct members of an object, well enough that I won't repeat them. But my executive summary would be: don't use smart pointers or dynamic allocation until something forces you to.