Thread: How is this possible?

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    How is this possible?

    I've heard from countless places the same thing on the scope of variables: once you enter a sub-scope and leave that sub-scope, any variables created or changes made to existing variables are destroyed.

    Is this not true? If it is, can somebody explain this?

    int factorial = 1;
    for (int x = 2; x <= num; ++x)
    factorial = factorial * x;
    cout << num <<"! = " << factorial <<"\n";


    Num being defined anywhere earlier, of course. Since "factorial = factorial * x;" is in the scope of the for loop, how is it possible that its value is carried out of that loop?

    Or are there certain rules about scopes that I have overlooked somehow?

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Is this not true?
    Yes:
    Code:
    int x = 0;
    
    if (true) {
      x = 12345;
    }
    
    cout << x; // Prints 12345
    But that's not true if you define a new variable in the nested scope with the same name. Then the inner name hides the outer name and changes made are to the inner name, which is destroyed when execution leaves the nested scope:
    Code:
    int x = 0;
    
    if (true) {
      int x = 12345;
    }
    
    cout << x; // Prints 0
    Just because I don't care doesn't mean I don't understand.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    So only variables defined in that scope are destroyed? Is it true in all cases that a variable defined outside a scope and used inside a sub-scope will, after the sub-scope has been executed, retain the value that was set to it, if there was any change, in the sub-scope?

  4. #4
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Is it true in all cases that a variable defined outside a scope and used inside a sub-scope will, after the sub-scope has been executed, retain the value that was set to it, if there was any change, in the sub-scope?
    Well, technically, yes. But apparently no, because the sub scope could be function, and by default, C++ is pass-by-value. In that case you'd make a copy, edit the copy, and the changes made won't be mirrored in the calling function. You can use a reference or a pointer to fix that though.
    Just because I don't care doesn't mean I don't understand.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    int factorial = 1;
    for (int x = 2; x <= num; ++x)
       factorial = factorial * x;
    cout << num <<"! = " << factorial <<"\n";
    
    //Since factorial is declared outside of the for-loop, it is still in scope here.
    //It will stay in scope until a } is encountered, probably at the end of this function.
    //This means any changes made to it are kept until a } is encountered.
    Last edited by swoopy; 08-30-2005 at 07:58 PM.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    Okie dokie, thanks for your help

    While you're here, could you explain if it is possible to and why using an increment or decrement operator would have a different effect on something than adding or subtracting 1 to or from it respectively?

  7. #7
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    it wouldn't have any different effect as an end result, if there is any difference it would be that ++ and -- compile to increment and decrement instructions. (most archs support them) instead of addition and subtraction instructions.

    ++ and -- only for convenience.

    edit: There is one thing you can do, the pre-increment and decrement operators for example:
    boat = theJunk[++i] ;
    would first increment i then set boat to theJunk[i] while:
    boat = theJunk[i++]
    would set boat to theJunk[i] and then increment i
    Last edited by valis; 08-30-2005 at 08:17 PM.

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    A decent optimising compiler would convert i = 1 + 1; to an INC op anyway.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed