Thread: multiple nonlocal variables

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    66

    multiple nonlocal variables

    Hello,

    I am reading a book and am curious about a statement it makes:

    "Anything declared in a block that contains a nested block is nonlocal to the inner block. (Global identifiers are nonlocal with respect to all blocks in the program.)"

    Code:
    int a = 1; //global, nonlocal
    
    void myFunction() {
       int a = 2; //local, hides global a
    
       while (a < 3) {
          a++; //a is nonlocal here?
       }
    }
    I always thought of "a" declared within myFunction as local to the function. But the book suggests that within the while statement it is nonlocal. Is this correct? How does C++ see a difference between two nonlocal variables? What exactly does C++ do when "name hiding"?

    EDIT: Just keeps reading backwards until it finds one.
    Last edited by Furious5k; 08-19-2011 at 06:54 PM.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Ok, so:
    1. int a =1 : This is declared outside of any block and thus has file/global scope.

    2. int a=2 in myFunction:

    This is declared inside the block (the function), thus has block scope which terminates at the end of the block - the end of the function in this case. Since the global and local variables have the same name, the inner scope (function scope) is visible and the outer scope (global) is hidden.

    3. The while loop: a is technically not local in the while block, however since the while block is inside the function block, a is visible to the while block since a has block scope for the entire function. You could technically declare another variable named a inside the while loop and it would be visible and hide the a declared by the function.

    A quick example program to demonstrate the rules of scope:
    Code:
    #include <iostream>
    
    int a = 2;
    
    void foo(void);
    
    int main(void){
    
    	std::cout <<"a in main is: " <<a <<std::endl;
    	foo();
    	std::cout<<"a after function call: " <<a<<std::endl;
    	std::cin.get();
    	return(0);
    }
    void foo(void){
    	int a = 3;
    	int b=4;
    	std::cout<<"a in foo is: " <<a <<std::endl;
    	while(b<5){
    		int a=5;
    		std::cout<<"a in while is: "<<a <<std::endl;
    		b++;
    	}
    	std::cout<<"a after while is: "<<a<<std::endl;
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    First of all, thank you for replying. I appreciate you taking time to answer my post and I do mean that. So I hope nobody takes this the wrong way, it is meant to be constructive criticism with a hint of humour:

    What was the question as written:
    "Is this right?"

    What you interpreted it to be:
    "How does scope work?"

    What it was:
    an insignificant double checking that I hadn't made some clearly obvious misreading of the text, in fact not a real question.

    What you gave as answer:
    160 word answer.

    What would have been a fitting answer:
    "Yes."

    What I wanted to know:
    "How does C++ see a difference between two nonlocal variables? What exactly does C++ do when "name hiding"?"

    I believe I have answered the first of those two questions myself. I am still curious about how "name hiding" works. And I don't mean what it does, I understand its function, I'm wondering about the implementation.
    Last edited by Furious5k; 08-23-2011 at 12:01 PM.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Furious5k View Post
    First of all, thank you for replying. I appreciate you taking time to answer my post and I do mean that. So I hope nobody takes this the wrong way, it is meant to be constructive criticism with a hint of humour:

    What was the question as written:
    "Is this right?"

    What you interpreted it to be:
    "How does scope work?"

    What it was:
    an insignificant double checking that I hadn't made some clearly obvious misreading of the text, in fact not a real question.
    What it was: You expressing your non-clear understanding of scope.
    What I did: Explain in clear terms what the standard says, not rewording of your book.

    Quote Originally Posted by Furious5k View Post
    What you gave as answer:
    160 word answer.

    What would have been a fitting answer:
    "Yes."
    See post above.

    What I wanted to know:
    Quote Originally Posted by Furious5k View Post
    "How does C++ see a difference between two nonlocal variables?
    What I did: Answered the question with my first post. Note the use of the word block scope.
    What you did: Not bother to read and apply my answer to your question.


    Quote Originally Posted by Furious5k View Post
    What exactly does C++ do when "name hiding"?"
    What every compiler does when resolving scope. The variable names you use are meaningless to the compiler. The process of compilation is a multiple step process. Part of that process is building a symbol tree in which the compiler will resolve block scope and thus it will know whether or not a particular variable is "in scope", e.g. can be seen.

    What I did: Just answered your question, however I ensured it was short for your convenience.
    What you are going to do: Not understand my answer, and then later decide that you already know the answer to this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    What it was: You expressing your non-clear understanding of scope.
    Could you please explain what part of scope I am unclear on?


    What you are going to do: Not understand my answer, and then later decide that you already know the answer to this.
    Even assuming I was absolutely wrong about everything I said, an honest attempt to point out another person's (perceived) mistake, probably doesn't deserve an insult in return.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Furious5k View Post
    Could you please explain what part of scope I am unclear on?
    You asked the question
    How does C++ see a difference between two nonlocal variables?
    which ... well, I still don't know what you actually want to know to answer that question. (The two variables named a differ in scope and are therefore different variables, so that's how the compiler tells them apart, I guess, maybe?)

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    A local variable name hides any nonlocal variable of the same name. So, if you have a local variable you use it, if not you use a nonlocal one.

    I interpreted this as being key to how the compiler decides on which variable to use. What confused me was that when you enter a new block, variables declared in previous block become nonlocal and this allows for the situation where you have two nonlocal declarations and no local ones. If it were a simple case of "use nonlocal" since local doesn't exist, the two declarations would be equivalent.

    So the real confusion stemmed from misunderstand that a variable being local and a variable having local scope are not tied. A variable can become nonlocal and still have local scope.

    I always thought of "a" declared within myFunction as local to the function.
    The book simply introduced the concept of a variable being local/nonlocal, which conflicted with the meaning I previously associated with a variable being local, which was a variable having local scope.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Furious5k View Post
    A local variable name hides any nonlocal variable of the same name. So, if you have a local variable you use it, if not you use a nonlocal one.
    Careful with this statement....

    Quote Originally Posted by Furious5k View Post
    I interpreted this as being key to how the compiler decides on which variable to use.
    Not quite.

    Quote Originally Posted by Furious5k View Post
    What confused me was that when you enter a new block, variables declared in previous block become nonlocal and this allows for the situation where you have two nonlocal declarations and no local ones. If it were a simple case of "use nonlocal" since local doesn't exist, the two declarations would be equivalent.
    This is because this is not how it works.

    Quote Originally Posted by Furious5k View Post
    So the real confusion stemmed from misunderstand that a variable being local and a variable having local scope are not tied. A variable can become nonlocal and still have local scope.
    Both statements are wrong. A local variable by definition has local scope, a non-local variable by definition cannot have local scope.

    Quote Originally Posted by Furious5k View Post
    The book simply introduced the concept of a variable being local/nonlocal, which conflicted with the meaning I previously associated with a variable being local, which was a variable having local scope.
    Which is why I initially made my "long winded" response to you.....

    There I kept my answers short this time.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Careful with this statement....
    That was the best way I could type it which wasn't untrue and reflected the way I was thinking about it earlier, though I was reading too much into it at the time.

    Not quite.
    This is because this is not how it works.
    I am aware. I was using past tense in sentences which were meant to represent what I thought before, to explain the context and hence meaning of my question.


    Both statements are wrong. A local variable by definition has local scope, a non-local variable by definition cannot have local scope.
    I don't understand. "a" has local scope within the whole of the function body, inclusive of the while loop, but within the loop it is "nonlocal"? So isn't there a point where it both has local scope and is "nonlocal"?

    Code:
    void myFunction() {
       int a = 2; //variable is local, has local scope
    
       while (a < 3) {
          a++; //variable is nonlocal, has local scope?
       }
    }

    Which is why I initially made my "long winded" response to you.....

    There I kept my answers short this time.
    It would be nice if you didn't belabour this.
    Also, I still feel you answered the difference between global and block scope variables, not two nonlocal ones neither of which needs to be global according to the quoted text.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    int a; //Let us call this a1, at file scope
    
    int main() {
        int a; //Let us call this a2; it is local, in local scope.  a1 is not in scope
        while (rand() < 10000) {
            a = a + 2; //This uses a2; it is nonlocal to this block, but it has local scope and is currently the only a in scope
            int a; //Let us call this a3; it is local, in local scope.  a2 is now not in scope, let alone in local scope
            a = a + 2; //This must use a3, as it is the only a in scope.
        }  //a3 no longer in scope, a2 once again in scope
        a = a + 2; //This must use a2, as it is the only a in scope.
    }
    //Poor a1, never used
    I think that covers the interesting cases for the question.

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Hehe, I like the last code comment.

    A local variable by definition has local scope, a non-local variable by definition cannot have local scope.
    So is this untrue then? Or is it a true definition and this is simply a pedantical technicality under which it doesn't hold up? or what?

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Furious5k View Post
    So is this untrue then? Or is it a true definition and this is simply a pedantical technicality under which it doesn't hold up? or what?
    It is very true. a2 in tabstop's example has function scope, which means it remains visible for the entire function, it is not local to the inner block (the while loop) although it is visible until the declaration of a3.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Tabstop's example is illustrative, though I wish he would do it with well defined code. The result of a+2 when a is uninitialized is undefined. Furthermore, on some, admittedly rare systems a+2 could result in an integer overflow causing an inturupt or structural exception to effectively crash the program.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It would probably be simpler to illustrate the point by modifying tabstop's sample so it prints out the address of a in various places, rather than trying to access or modify its value.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  15. #15
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    From a couple of sources:
    "The only type of identifier with function scope is a label name." I'm not sure why this term is now being used.

    This is how I'm understanding what you mean by function scope:
    Function scope has the same meaning as local/block scope, except you have to be talking about a function block when using the term, no? So not all local scope is function scope, but all function scope is also local scope.

    a non-local variable by definition cannot have local scope
    a = a + 2; //This uses a2; it is nonlocal to this block, but it has local scope
    The way I read it, you seem to be saying a2 doesn't have local scope, rather having function scope. But searching for function scope gave me no definition to support this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can this be done - multiple variables stored
    By spadez in forum C Programming
    Replies: 2
    Last Post: 04-14-2009, 10:13 AM
  2. comparing multiple variables..
    By myrddin in forum C Programming
    Replies: 9
    Last Post: 10-16-2007, 07:03 PM
  3. Multiple variables in sprintf
    By 3saul in forum C Programming
    Replies: 4
    Last Post: 02-15-2006, 09:25 PM
  4. Multiple variables
    By Kaho in forum C Programming
    Replies: 2
    Last Post: 08-18-2005, 04:32 AM
  5. multiple instances and variables
    By FOOTOO in forum Windows Programming
    Replies: 1
    Last Post: 04-07-2005, 10:54 AM