Local Variables

Started by
5 comments, last by Julian90 17 years, 6 months ago
Hi guys, Take a look at this: void MyFunction(BOOL flag) { int a,b; // I know these get allocated if(flag){ int c,d; // but what about these if flag=FALSE ? } } Thanks.
Advertisement
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
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
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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 :)
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 :-)

This topic is closed to new replies.

Advertisement