简体   繁体   中英

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?

Also, is the answer to this question the same for the C language?

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.

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)

At this point, there is finally a catch handler with only myVector still in scope. That block ignores the exception, and then main ends-- at THAT point myVector is destructed.

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 :

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

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

为了避免使用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.

In C, it is the simple case of moving up the stack pointer to get rid of the variables held.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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