×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Scope of variable defined inside a block in C++

Scope of variable defined inside a block in C++

Scope of variable defined inside a block in C++

(OP)
Hi,

Suppose I have the following piece of code

CODE

int main()
{
 int A;
 //expressions

   if
   {
      int B;
      //expressions
   }
   else
   {
     //expressions
   }

   return 0;
}

My question is

1. Is it valid in C++ to define a variable such as the integer B inside the "if" block?
2. Can we use this integer outside of the "if" block?

RE: Scope of variable defined inside a block in C++

1. Yes.
2. No.

RE: Scope of variable defined inside a block in C++

Just a little explanation: it's not if block declaration. You declare B in a [b]compound statement or block[b] (in {...}, see C++ Standard).

RE: Scope of variable defined inside a block in C++

(OP)
Thanks for your quick reply, cpjust. And to ArkM,I am new to C++, so not really familiar with the terminology and some concept, thanks for pointing out that.

One more question is

if i declare another global variable int B, after the if/else statement, would it cause any problem? I mean would the compiler delete or release the memory allocated to the int B declared inside the if statement?


Thanks

RE: Scope of variable defined inside a block in C++

Declaring int B after the if/else statement would be a local variable, not a global one. It will not cause any problems, because there is no variable B known at that point of declaration. Local variables are allocated on the stack, the compiler does not delete or release them, nor does it have to. Only the stack pointer is adjusted when the variable goes out of scope.
Only when you create a local variable which is a pointer you must free or release it yourself before it goes out of scope.
Example:

CODE

Something *pSomething = new Something();
// new Something() has created the class instance on the heap
// and not on the stack.
// On the stack is only the address of the instance.
// So we need to release pSomething before it goes out of scope
// Otherwise the space on the heap will never be freed and there
// is a memory leak
delete pSomething;

compare that with the following:

CODE

Something something;
// Now the entire class instance is on the stack
// No need to release something before it goes out of scope

Marcel

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close