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 7984133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:10:13+00:00 2026-06-04T11:10:13+00:00

If I define a variable inside the block of a control structure, does it

  • 0

If I define a variable inside the block of a control structure, does it exist only in the execution of that control structure’s block and not in the whole execution of the enclosing function?

also how can I monitor the exact memory usage of my programs and its changes (i.e.: changes in memory usage by creating and destroying variables)?

added later:
in following code I know v scope is if block, but I want to know whether v is created/destroyed in the memory at the start/end of the if block or at the start/end of the function func?

void func ()
{
    if (true)
    {
        int v;//automatic storage class
        v = 1;
    }
}
  • 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-06-04T11:10:15+00:00Added an answer on June 4, 2026 at 11:10 am

    If I define a variable inside the block of a control structure, does it exist only in the execution of that control structure’s block and not in the whole execution of the enclosing function?

    It depends on where you declare the variable not define it.

    The variable is only accessible in the scope where you declare it. It can be accessed beyond tht scope if you explicitly pass it around but if it remains valid would depend on the storage type of the variable.
    For eg: static variables remain alive throughout the program lifetime, while,
    returning address of an automatic variable from an function would result in Undefined Behavior because the variable does not remain valid after the function returns.

    Good Read:
    What is the difference between a definition and a declaration?

    How can I monitor the exact memory usage of my programs and its changes (i.e.: changes in memory usage by creating and destroying variables)?

    I believe you would want to get information on dynamically allocated objects because automatic objects only live long enough in their scope, they will automagically destroyed so they don’t usually cause any problems.

    For dynamic objects You can use memory profiling tools like valgrind with Massif or you could replace new and delete operators for your class and collect the diagnostic information.


    EDIT: To address the updated Q.

    In following code I know v scope is if block, but I want to know whether v is created/destroyed in the memory at the start/end of the if block or at the start/end of the function func?

    v is created when the scope in which it is declared starts and the statement where it is declared gets executed. v is destroyed once the scope ends i.e } is reached.
    This concept is used for forming the basis of one of the most widely used concepts in C++ known as Resource Allocation is Initialization(RAII). Every C++ programmer absolutely must know about it.

    It is simple to demonstrate and verify the creation and destruction of the objects through this small modified code sample:

    #include<iostream>
    class Myclass
    {
        public:
            Myclass(){std::cout<<"\nIn Myclass Constructor ";}
            ~Myclass(){std::cout<<"\nIn Myclass Destructor";}
    };
    
    void func()
    {
        std::cout<<"\nBefore Scope Begins";
        if (true)
        {
            Myclass obj;//automatic storage class
        }
        std::cout<<"\nAfter Scope Ends";
    }
    
    int main()
    {
        std::cout<<"\nBefore Calling func()";
        func();
        std::cout<<"\nAfter Calling func()";
        return 0;
    }
    

    The output is:

    Before Calling func()
    Before Scope Begins
    In Myclass Constructor
    In Myclass Destructor
    After Scope Ends
    After Calling func()

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

Sidebar

Related Questions

When we 'define' a variable inside a function (not main here), is the memory
I'm trying to define variable inside function. vars() shows variable is created, but gives
How can I define a static member variable that is also thread local inside
What are the reasons behind that we can not declare and define a variable(property)
Why can't i define a variable recursively in a code block? scala> { |
Is there any way to define a variable that can be used in multiple
Hi All is there any way to locally define a variable in a function
I have a variable that im define with another variable and some text. $title
I would like to have a variable (or #define ) in C++ source that
I have a variable that is not supposed to change its value after it's

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.