Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6028411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:44:09+00:00 2026-05-23T04:44:09+00:00

In C++, if we declare a stack variable inside of a function, is it

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:44:10+00:00Added an answer on May 23, 2026 at 4:44 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i declare a global variable and use and modify its value in the function.
Normally, in Delphi one would declare a function with a variable number of arguments
this is my first question on stack overflow, so be gentle. Let me first
There has been asked that question already. The answer was Stack space for local
I need to declare a variable inside an if statement. Then I will use
I declare a variable for a 64 bit counter as : long long call_count;
I declare a static char array, then I pass it to a function. How
I think this maybe a really dumb question, but i just want to be
There is a JDK function that, although the javadocs does not declare it as
Sorry if this seems like a dumb question but I am just learning bash

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.