我什么时候应该在 C++ 中使用 new 关键字? [英] When should I use the new keyword in C++?

查看:35
本文介绍了我什么时候应该在 C++ 中使用 new 关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 C++ 有一段时间了,我一直对 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. 使用 new 关键字...
  1. With the new keyword...

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

  1. 如果没有 new 关键字...
  1. Without the new keyword...

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

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

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.

困难似乎是方法 1 更难与 std C++ 类一起使用.

The difficulty seems to be that method 1 is harder to use with the std C++ classes.

我应该使用哪种方法?

我最近将 new 关键字用于内存(或空闲存储),用于超出范围的大型数组(即从函数返回).在我使用堆栈之前,它导致一半元素在范围之外损坏,切换到堆使用确保元素完好无损.耶!

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 intact. Yay!

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

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).

推荐答案

方法一(使用new)

Method 1 (using new)

  • 免费存储上的对象分配内存(这通常与相同)
  • 要求您稍后明确删除您的对象.(如果你不删除它,你可能会造成内存泄漏)
  • 内存保持分配状态,直到您删除它.(即您可以返回一个您使用new创建的对象)
  • 问题中的示例将泄漏内存,除非指针是delete代码>d;并且它应该总是被删除,无论采用哪个控制路径,或者是否抛出异常.
  • 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 could return an object that you created using new)
  • The example in the question will leak memory unless the pointer is deleted; and it should always be deleted, regardless of which control path is taken, or if exceptions are thrown.

方法二(不使用new)

Method 2 (not using new)

  • 堆栈上的对象分配内存(所有局部变量所在的位置) 堆栈可用的内存通常较少;如果分配的对象过多,则会面临堆栈溢出的风险.
  • 您以后无需删除它.
  • 超出范围时不再分配内存.(即,您不应该return 指向堆栈上的对象的指针)
  • 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.

一些简单的案例:

  • 如果您不想担心调用 delete,(以及可能导致 内存泄漏) 你不应该使用 new.
  • 如果你想从一个函数返回一个指向你的对象的指针,你必须使用 new
  • If you don't want to worry about calling delete, (and the potential to cause memory leaks) you shouldn't use new.
  • If you'd like to return a pointer to your object from a function, you must use new

这篇关于我什么时候应该在 C++ 中使用 new 关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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