简体   繁体   English

在C ++中删除堆栈变量

[英]Deletion of stack variable in C++

In C++, if we declare a stack variable inside of a function, is it automatically deleted at the end of the function or is it deleted at the end of the program execution? 在C ++中,如果我们在函数内部声明一个堆栈变量,它是在函数末尾自动删除还是在程序执行结束时删除?

Also, is the answer to this question the same for the C language? 此外,对于C语言,这个问题的答案是否相同?

For stack-declared variables, the destructor is called and the memory reclaimed as it falls out of scope. 对于堆栈声明的变量,将调用析构函数并在内存超出范围时回收内存。

Note that this doesn't mean at the end of the function if the variable is declared in an inner block, like an if-statement or loop. 请注意,如果变量是在内部块中声明的,如if语句或循环,则这并不意味着在函数的末尾。

int main(int argc, char **argv)
{
    int a = 3;

    if (argc > 1)
    {
        int b = 5;
        ++b;
    } // b is destructed here

    // a is destructed here
    // argv and argc are destructed here (or with a)
}

EDIT : A good point was made about the fact that it doesn't matter how the scope is exited. 编辑 :关于如何退出范围无关紧要的事实是一个很好的观点。 So... 所以...

#include <vector>
# include <exception>

int main(int argc, char **argv)
{
    std::vector<int> myVector(10);

    try
    {
        if (argc)
        {
            int a = 10;
            int b = 12;
            for (int c = 0; c < b; c++) // lol
            {
                int c_squared = c*c;
                int theEleventhElement = myVector.at(c);
                // the above will throw std::out_of_range at some point!
            }
        }
    }
    catch (std::out_of_range &ex)
    {
        // do nothing
    }
}

When the above throws, the stack will unwind as part of the exception handling. 当上面抛出时,堆栈将作为异常处理的一部分展开。 Thus, the variables will be destroyed in the following order: 因此,变量将按以下顺序销毁:

  • c_squared
  • c
  • b and a (I think in that order, but I don't know if that's mandated in the standard) ba (我想按顺序,但我不知道这是否符合标准)

At this point, there is finally a catch handler with only myVector still in scope. 此时,最后一个catch处理程序只有myVector仍在范围内。 That block ignores the exception, and then main ends-- at THAT point myVector is destructed. 该块忽略异常,然后main在该点ends-- myVector被破坏。

As per 3.7.2 objects with automatic storage duration last until the exit of the block in which they were created , There are more details in 6.6.2 : 根据具有自动存储持续时间的3.7.2对象, 直到创建它们的块的退出为止 ,在6.6.2中有更多细节:

On exit from a scope ( however accomplished ), destructors (12.4) are called for all constructed objects with automatic storage duration (3.7.2) (named objects or temporaries) that are declared in that scope, in the reverse order of their declaration. 在从范围退出( 但是已完成 )时,将对所有具有自动存储持续时间(3.7.2)(命名对象或临时值)的构造对象调用析构函数(12.4),这些对象在其范围内声明,其顺序与其声明相反。 Transfer out of a loop, out of a block, or back past an initialized variable with automatic storage duration involves the destruction of variables with automatic storage duration that are in scope at the point transferred from but not at the point transferred to. 从循环中移出一个循环,或者从具有自动存储持续时间的初始化变量返回过去涉及销毁具有自动存储持续时间的变量,这些变量在转移点但不在转移点处的范围内。

It is destroyed when it goes out of scope. 它超出范围时会被销毁。 Same for C. C相同

自动变量在其封闭范围的末尾自动销毁。

为了避免使用new运算符的歧义,可能更合适的做法是在离开函数时“弹出”变量:参见基于堆栈的内存分配 .C也是如此。

A stack variable is more or less a couple of bytes shaved off the stack by decrementing the stack pointer, it is deleted at the end of the function, however, not simply moving the stack pointer up again (the case of user-defined types) in C++ because of the extra destruction stuff. 堆栈变量或多或少是通过递减堆栈指针从堆栈中削减的几个字节,在函数结束时删除它,而不是简单地再次向上移动堆栈指针(用户定义类型的情况)在C ++中因为额外的破坏性东西。

In C, it is the simple case of moving up the stack pointer to get rid of the variables held. 在C中,移动堆栈指针以摆脱所持有的变量是一个简单的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM