C++析构函数:什么时候释放内存?

发布于 2024-10-13 15:01:45 字数 58 浏览 4 评论 0原文

如果我删除一个导致其析构函数被调用的对象,那么内存是在析构函数完成函数中的任何操作之前还是之后被释放?

If I delete an object which causes its destructor to be called, does the memory get freed before or after the destructor has finished doing whatever there is in the function?

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

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

发布评论

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

评论(6

給妳壹絲溫柔 2024-10-20 15:01:45

仅当最小派生类子对象被销毁后才会释放内存。因此,如果您有:

class Base {
};

class Derived : public Base {
public:
    ~Derived();
};

那么首先销毁Derived,然后销毁Base,然后才释放内存。

Memory is only freed once the least derived class subobject has been destroyed. So if you have:

class Base {
};

class Derived : public Base {
public:
    ~Derived();
};

then first Derived is destroyed, then Base is destroyed and only then memory is deallocated.

路还长,别太狂 2024-10-20 15:01:45

delete分解为它实际在做什么,可以比较清楚地看到内存什么时候被删除。所以像这样的语句:

delete some_ptr;

大致相当于这个伪代码:

some_ptr->~some_ptr();
free( some_ptr );

所以在调用析构函数后内存被释放。析构函数的确切作用不是由删除运算符决定的,而是由类的定义决定的。通常它会进行本地清理并确保其基类析构函数也被调用。

重要的是要认识到释放内存实际上并不是析构函数的一部分。释放内存的是delete操作符。


请注意,伪代码中的free 函数实际上是operator delete() 函数之一,无论是针对已删除的类,还是全局的。这实际上释放了内存。

Decompose delete into what it is actually doing and it is relatively clear to see when the memory is deleted. So a statement like this:

delete some_ptr;

Is roughly equivalent to this pseudo-code:

some_ptr->~some_ptr();
free( some_ptr );

So the memory is freed after the call to the destructor. Exactly what the destructor does is not determined by the delete operator, but rather the definition of the class. Usually it does local cleanup and ensures that its base class destructors are also called.

It is important to realize that freeing the memory is not actually part of the destructor. It is the delete operator which frees the memory.


Note that the free function in pseudo-code is actually one of the operator delete() functions, either for the deleted class, or global. That actually frees up the memory.

兔姬 2024-10-20 15:01:45

析构函数完成后,内存将被释放。否则,访问析构函数内的成员变量将导致段错误。

The memory gets freed after the destructor has finished. Otherwise, accessing member variables inside the destructor would cause segfaults.

风吹雨成花 2024-10-20 15:01:45

运算符delete在析构函数之后调用,但是何时释放内存取决于使用的分配器

operator delete is called after destructor, but when the memory is freed is up to used allocator

回眸一遍 2024-10-20 15:01:45

我认为析构函数本身执行完毕后内存就会被释放。我知道,当捕获异常时,直到对象本身超出范围时才会调用对象的析构函数。

I would think that the memory is freed after the destructor function itself has finished executing. I know that when an exception is caught, the destructor to the object is not called until the object itself goes out of scope.

听风念你 2024-10-20 15:01:45

在 C++ 中,销毁是使用对象中可用的数据执行一些代码。这段代码是任意的。

释放内存是一种低级处理,通常被删除操作符隐藏,永远不应该在调用析构函数之前调用它。

Allocator 接口对此进行了最好的总结:

  • allocatedeallocate 用于操作原始内存
  • constructdestroy 用于调用对象的构造函数和析构函数

确切地说,>constructdestroydeallocate 只能在该分配器先前分配的内存上执行。它还明确指出 destroy 不会释放内存,并且需要随后调用 deallocate

请注意,这是一个低级接口,它允许销毁一个对象并重用释放的空间来构造另一个对象。

In C++, destruction is about executing some code using the data available in the object. This code is arbitrary.

Free'ing the memory is a low level handling, hidden by the delete operator in general, that should never be called prior to calls to the destructor.

This is best summarized by the Allocator interface:

  • allocate and deallocate are used to manipulate raw memory
  • construct and destroy are used to call the constructors and destructors of the objects

It is precised that construct, destroy and deallocate should only be executed on memory previously allocated by that allocator. It also precises that destroy does not deallocate the memory, and that a subsequent call to deallocate will be necessary.

Note that this is a low-level interface, which allow destroying an object and reusing the freed space to construct another in place.

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