Sign in to follow this  

Local Variables

This topic is 4069 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

void MyFunction(BOOL flag)
{
int a,b; // I know these get allocated

if(flag)
{
int c,d; // but what about these if flag=FALSE ?
}
}


Your question is really about scope each { starts a new scope and each } ends a scope. When you create variables they ONLY exist in the scope that they were allocated in and scopes that are inside that scope. In addition they are ALL alocated at the START of that scope and destroyed at the END of the scope REGARDLESS of were you declare the variables.

So getting to your question when it gets to the if c and d will only be allocated if the scope they are in is entered which will only happen if the if statment evaluates to true.

Hope that helps

Share this post


Link to post
Share on other sites
Quote:
Original post by Julian90
In addition they are ALL alocated at the START of that scope and destroyed at the END of the scope REGARDLESS of were you declare the variables.


I believe that is not true for C++. Consider a (rather stupid, but for example purpose) class like:


struct StupidClass
{
StupidClass() { Sleep(10000); }
}

void SomeFunc(bool earlyExit)
{
if(earlyExit)
return;

StupidClass sc;
// stuff..
}


while sc will be existing in the scope of "SomeFunc", it doesnt exist before it is declared. This is why it's often advised to programmers to declare and instantiate their objects as late as possible, and as close as possible from the point they need them

- JA

Share this post


Link to post
Share on other sites
In general it is a good idea to give each variable the smallest scope necessary. If the scope of certain variables within a function don't overlap then there is a reasonable chance that the compiler may generate code that uses the same area of the stack frame for both variables.

In this case given the use of POD types it might even be able to reuse the same stack frame location when the scopes do overlap, so long as if you don't use a or b after the "if (flag)" line. However with so few variables it is more likely that these things would go in registers anyway.

Of course if you don't use the variables then they will probably be omitted too.

Share this post


Link to post
Share on other sites
Quote:
Original post by janta
while sc will be existing in the scope of "SomeFunc", it doesnt exist before it is declared. This is why it's often advised to programmers to declare and instantiate their objects as late as possible, and as close as possible from the point they need them

- JA
You have to be careful about what you mean where you say 'exists'. It quite likely has stack space allocated for it at the start of its scope, but it is not actually constructed until the declaration is reached.
Destruction always occurs right at the end of the scope though.

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
You have to be careful about what you mean where you say 'exists'. It quite likely has stack space allocated for it at the start of its scope, but it is not actually constructed until the declaration is reached.
Destruction always occurs right at the end of the scope though.

Ah, that sounds good to know :)

Share this post


Link to post
Share on other sites
Quote:
Original post by janta
Quote:
Original post by iMalc
You have to be careful about what you mean where you say 'exists'. It quite likely has stack space allocated for it at the start of its scope, but it is not actually constructed until the declaration is reached.
Destruction always occurs right at the end of the scope though.

Ah, that sounds good to know :)


yes sorry i should have been clearer that is what i meant by exists (the stack space being allocated) but the compiler still wont let you use it until after it is declare :-)

Share this post


Link to post
Share on other sites

This topic is 4069 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this