rrazd
rrazd

Reputation: 1739

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?

Upvotes: 2

Views: 362

Answers (6)

Platinum Azure
Platinum Azure

Reputation: 46183

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.

Upvotes: 6

lccarrasco
lccarrasco

Reputation: 2051

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.

Upvotes: 3

Khaled Nassar
Khaled Nassar

Reputation: 884

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.

Upvotes: 0

Seth Johnson
Seth Johnson

Reputation: 15172

To avoid the ambiguity with the new operator, it might be more appropriate to say that the variable is "popped" when leaving a function: see stack-based memory allocation.The same goes for C.

Upvotes: 0

fredoverflow
fredoverflow

Reputation: 263118

Automatic variables are automatically destroyed at the end of their enclosing scope.

Upvotes: 2

Fred Larson
Fred Larson

Reputation: 62063

It is destroyed when it goes out of scope. Same for C.

Upvotes: 2

Related Questions