删除主新变量中的新变量

发布于 2024-12-29 23:27:49 字数 239 浏览 0 评论 0原文

抱歉,关于标题。我不知道该给它起什么名字。如果有任何模组正在阅读并且他们理解了这个问题,那么如果需要的话也请重命名。

假设您创建一个变量 (varOne)。

在 varOne 代码中,其他变量被创建为 new (varTwo, varThree)。

如果您对 varOne 调用 delete,varTwo 和 varThree 是否会被删除,或者您是否需要删除它们并删除 varOne?

Sorry about the title. I wasnt sure what to name it. If any mods are reading and they understand the question then please rename if needed too.

Say you create a new variable (varOne).

Inside the varOne code, other variables are created as new (varTwo, varThree).

If you call delete on varOne, will varTwo and varThree be deleted, or do you need to delete them AND delete varOne?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

感受沵的脚步 2025-01-05 23:27:50

我不知道如何理解你的问题,因为你不是 new 变量(所有变量都是静态、自动或成员变量),而是对象(从 new< 获得的指针)然而 /code> 通常会分配给 use 用于初始化变量,也许这就是你的意思?)。因此,我将给出一个一般性的答案,希望包含您所要求的内容。

首先,作为基本规则,使用 new 分配的每个对象都必须使用 delete 显式释放。但是,delete 可能隐藏在另一个对象中,例如 boost 或 C+ 中的 shared_ptrscoped_ptr/unique_ptr +11,或早期版本的 C++ 中的 auto_ptr

如果你的对象包含子对象,通常最好让它们成为直接成员,这样你就根本不用 new 来分配它们(事实上,这是 C++ 中的一般规则:如果你绝对没有动态分配,不要)。也就是说,您可以将类编写为,

class X
{
public:
  // ...
private:
  Type1 subobject1;
  Type2 subobject2:
};

并且根本不必为子对象使用 new/delete 。但是,如果您需要动态分配对象,则还必须删除它们,例如,

class X
{
public:
  X()
  {
    subobject1 = new Type1();
    try
    {
      subobject2 = new Type2();
    }
    catch(...)
    {
      delete subobject1;
    }
  }
  ~X()
  {
    delete subobject2;
    delete subobject1;
  }
  // ...
private:
  X(X const&); // disabled
  X& operator=(X const&); // disabled
  Type1* subobject1;
  Type2* subobject2;
};

请注意 X 构造函数中相当复杂的代码,以确保即使在出现异常的情况下也能正确清理对象。另请注意,您还必须实现复制构造和赋值,或者通过将它们设置为私有和未实现来禁用它们(请注意,C++11 提供特殊语法 = delete 来禁用它们)。通过使用智能指针,你可以省去很多麻烦(但你仍然需要注意复制构造和赋值,至少使用通常的智能指针):

class X
{
public:
  X():
    subobject1(new Type1()),
    subobject2(new Type2())
  {
  }
private:
  X(X const&) = delete; // disabled
  X& operator=(X const&) = delete; // disabled
  std::unique_ptr<Type1> subobject1;
  std::unique_ptr<Type2> subobject2;
};

这里我使用了 C++11 的 unique_ptr (因此还使用 C++11 语法来删除复制构造函数和赋值运算符)。请注意,乍一看,这段代码似乎根本没有删除;然而,这些删除实际上隐藏在unique_ptr的析构函数中。另请注意,现在不再需要构造函数中的显式异常处理;由于删除是在 unique_ptr 的析构函数中完成的,因此 C++ 的构造函数异常处理规则会自动处理此问题。

I'm not sure how to understand your question, since you don't new a variable (all variables are static, automatic or member variables) but objects (the pointers you get from new will however usually assigned to use used to initialize variables, maybe that's what you meant?). Therefore I'll give a general answer ad hope that what you asked for is included.

First, as a basic rule, every object you allocate with new has to be deallocated explicitly with delete. However, the delete might be hidden in another object, like shared_ptr and scoped_ptr/unique_ptr from boost or C++11, or auto_ptr in earler versions of C++.

If your object contains subobjects, it's usually best to make them direct members, so you don't allocate them with new at all (indeed, that's a general rule in C++: If you don't absolutely have to dynamically allocate, don't). That is, you'd write your class as

class X
{
public:
  // ...
private:
  Type1 subobject1;
  Type2 subobject2:
};

and don't have to mess with new/delete for the sub objects at all. However if you need to dynamically allocate the objects, you also have to delete them, e.g.

class X
{
public:
  X()
  {
    subobject1 = new Type1();
    try
    {
      subobject2 = new Type2();
    }
    catch(...)
    {
      delete subobject1;
    }
  }
  ~X()
  {
    delete subobject2;
    delete subobject1;
  }
  // ...
private:
  X(X const&); // disabled
  X& operator=(X const&); // disabled
  Type1* subobject1;
  Type2* subobject2;
};

Note the rather complicated code in X's constructor to make sure the object is correctly cleaned up even in case of an exception. Also note that you also have to implement copy construction and assignment or disable them by making them private and unimplemented (note that C++11 offers the special syntax = delete to disable them). You can save yourself a lot of the trouble by using a smart pointer (but you still have to take care about copy construction and assignment, at least with the usual smart pointers):

class X
{
public:
  X():
    subobject1(new Type1()),
    subobject2(new Type2())
  {
  }
private:
  X(X const&) = delete; // disabled
  X& operator=(X const&) = delete; // disabled
  std::unique_ptr<Type1> subobject1;
  std::unique_ptr<Type2> subobject2;
};

Here I've used C++11's unique_ptr (and consequently also used C++11 syntax for removing copy constructor and assignment operator). Note that on first impression this code seems to have no delete at all; however those deletes are actually hidden in the destructor of unique_ptr. Also note that now the explicit exception handling in the constructor is no longer needed; since the deleting is done in the destructors of the unique_ptrs, C++'s exception handling rules for constructors automatically take care of this.

放血 2025-01-05 23:27:49

您只需要删除 varTwo 和 varThree,因为当您退出 varOne 的析构函数时,用于调用 varOne 的析构函数的删除将清除 varOne 的该实例。

换句话说,在下面的示例中,varOne 是 Foo,varTwo 是 m_i,varThre 是 m_c:

class Foo
{
public:
  Foo() : m_i( new int ), m_c( new char ) { }
  ~Foo() { delete m_i; delete m_c; }
  // don't forget to take care of copy constructor and assignment operator here!!!
private:
  int*  m_i;
  char* m_char;
};

main()
{
  Foo* p = new Foo;
  delete p;
}

还要确保执行此操作时遵循 三法则 否则你的程序将会遇到内存问题。 (换句话说,如果您在构造函数中进行内存分配,请确保覆盖或删除默认的复制构造函数和赋值运算符)。

You only need to delete varTwo and varThree, because when you fall out of varOne's destructor, the delete you used to invoke varOne's destructor will clean up that instance of varOne.

In other words, in the example below, varOne is Foo, varTwo is m_i, and varThre is m_c:

class Foo
{
public:
  Foo() : m_i( new int ), m_c( new char ) { }
  ~Foo() { delete m_i; delete m_c; }
  // don't forget to take care of copy constructor and assignment operator here!!!
private:
  int*  m_i;
  char* m_char;
};

main()
{
  Foo* p = new Foo;
  delete p;
}

Also make sure that when you do this, you follow The Rule of Three or your program will suffer memory problems. (In other words, if you are doing memory allocation in your constructor, be sure you either override or delete the default copy-constructor and assignment operators).

蛮可爱 2025-01-05 23:27:49

您必须删除它们并单独删除 varOne,但实际上 varOne 的构造函数应该分配这些变量,并且如果它们必须在堆上某些时间,析构函数应该释放它们原因。最好只按值存储它们,并永久删除 newdelete

You have to delete them and delete varOne seperately, but actually the constructor of varOne should allocate those variables and the destructor should deallocate them if they have to be on the heap for some reason. It would be better to just store them by value and be rid of new and delete for good.

So尛奶瓶 2025-01-05 23:27:49

我不是 100% 确定你的意思,但一般来说,你用 new 分配的任何东西,你都必须用 delete 单独解除分配。

如果您的意思是在 C++ 类的上下文中,则需要手动删除析构函数的 varOne 和 varTwo。

I'm not 100% sure what you mean, but in general, anything that you allocate with new, you have to individually deallocate with delete.

If you mean this in the context of a C++ class, you will need to manually delete varOne and varTwo of the destructor.

夜声 2025-01-05 23:27:49

使用智能指针,永远不要删除您自己代码中的任何内容。

Use a smart pointer, and never ever ever delete anything in your own code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文