当对象超出范围时,是否调用析构函数? [英] Is a destructor called when an object goes out of scope?

查看:194
本文介绍了当对象超出范围时,是否调用析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

int main() {
    Foo *leedle = new Foo();

    return 0;
}

class Foo {
private:
    somePointer* bar;

public:
    Foo();
    ~Foo();
};

Foo::~Foo() {
    delete bar;
}

析构函数是由编译器隐式调用还是会有内存泄漏?

Would the destructor be implicitly called by the compiler or would there be a memory leak?

我是动态内存的新用户,所以如果这不是一个可用的测试用例,对不起。

I'm new to dynamic memory, so if this isn't a usable test case, I'm sorry.

推荐答案

是的,自动变量将在封闭代码块的末尾被销毁。但继续阅读。

Yes, automatic variables will be destroyed at the end of the enclosing code block. But keep reading.

您的问题标题询问是否在变量超出范围时调用析构函数。大概你打算问的是:

Your question title asks if a destructor will be called when the variable goes out of scope. Presumably what you meant to ask was:


会在main()结束时调用Foo的析构函数。

will Foo's destructor be called at the end of main()?

由于您提供的代码,该问题的答案是没有,因为Foo对象具有动态存储持续时间,我们将很快看到。

Given the code you provided, the answer to that question is no since the Foo object has dynamic storage duration, as we shall see shortly.

请注意自动变量是什么:

Note here what the automatic variable is:

Foo* leedle = new Foo();

这里, leedle 将被销毁。 leedle 只是一个指针。 leedle 指向不是具有自动存储持续时间,并且不会被销毁。所以,如果你这样做:

Here, leedle is the automatic variable that will be destroyed. leedle is just a pointer. The thing that leedle points to does not have automatic storage duration, and will not be destroyed. So, if you do this:

void DoIt()
{
  Foo* leedle = new leedle;
}

您泄漏由 new leedle

You leak the memory allocated by new leedle.

必须 code>已分配的任何内容:

void DoIt()
{
  Foo* leedle = new leedle;
  delete leedle;
}






并且通过使用智能指针更健壮。在C ++ 03中:


This is made much simpler and more robust by using smart pointers. In C++03:

void DoIt()
{
  std::auto_ptr <Foo> leedle (new Foo);
}

或在C ++ 11中:

Or in C++11:

void DoIt()
{
  std::unique_ptr <Foo> leedle = std::make_unique <Foo> ();
}

智能指针用作自动变量,如上所述,的范围并被销毁,它们会自动(在析构函数中) delete 被指向的对象。因此,在上述两种情况下,都没有内存泄漏。

Smart pointers are used as automatic variables, as above, and when they go out of scope and are destroyed, they automatically (in the destructor) delete the object being pointed to. So in both cases above, there is no memory leak.

让我们在这里清除一点语言。在C ++中,变量具有存储持续时间。在C ++ 03中,有3个存储持续时间:

Let's try to clear up a bit of language here. In C++, variables have a storage duration. In C++03, there are 3 storage durations:

1:自动:自动存储持续时间的变量将在最后的包围代码块。

1: automatic: A variable with automatic storage duration will be destroyed at the end of the enclosing code block.

请考虑:

void Foo()
{
  bool b = true;
  {
    int n = 42;
  } // LINE 1
  double d = 3.14;
} // LINE 2

在此示例中,所有变量都有自动存储持续时间。 b d 将在LINE 2处销毁。 n 将在LINE 1处销毁。

In this example, all variables have automatic storage duration. Both b and d will be destroyed at LINE 2. n will be destroyed at LINE 1.

2: static :将在程序开始之前分配具有静态存储持续时间的变量,当程序结束时。

2: static: A variable with static storage duration will be allocated before the program begins, and destroyed when the program ends.

3:动态:使用动态内存分配函数分配具有动态存储持续时间的变量(例如 new ),并且在使用动态内存分配函数(例如 delete )销毁它时会被销毁。

3: dynamic: A variable with dynamic storage duration will be allocated when you allocate it using dynamic memory allocation functions (eg, new) and will be destroyed when you destroy it using dynamic memory allocation functions (eg, delete).

在我上面的原始示例中:

In my original example above:

void DoIt()
{
  Foo* leedle = new leedle;
}

leedle 变量具有自动存储持续时间,并将在末端括号中销毁。 leedle 指向具有动态存储持续时间,并且不在上面的代码中销毁。您必须调用 delete 才能解除分配。

leedle is a variable with automatic storage duration and will be destroyed at the end brace. The thing that leedle points to has dynamic storage duration and is not destroyed in the code above. You must call delete to deallocate it.

C ++ 11还会添加第四个存储持续时间:

C++11 also adds a fourth storage duration:

4:线程:线程开始时分配具有线程存储持续时间的变量,线程结束时释放。

4: thread: Variables with thread storage duration are allocated when the thread begins and deallocated when the thread ends.

这篇关于当对象超出范围时,是否调用析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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