Where do they go?

This is a discussion on Where do they go? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by iMalc You've actually picked a particularly complex example there. It depends on what compiler and what compiler ...

  1. #16
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by iMalc View Post
    You've actually picked a particularly complex example there. It depends on what compiler and what compiler settings are chosen. The scope of a for-loop variable used to be greater than the body of the loop, but in modern compilers that aren't set to a compatibility mode, it is just inside the loop.
    Thus on newer compilers, if you had three such loops one after the other, it could use the same memory location for each for loop variable if it wanted to. Of course it might not even use a memory location at all, since it might be happy just holding the value in a register.
    Or the compiler can unroll the loop completely, meaning that there is never even a register used for the loop counter. All sorts of strange things happen when you start relying on undefined behavior

  2. #17
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by KCfromNC View Post
    Or the compiler can unroll the loop completely, meaning that there is never even a register used for the loop counter. All sorts of strange things happen when you start relying on undefined behavior
    I'm not sure what you mean by undefined behavior. The code will operate precisely as written in the source, although the compiler may do all kinds of things that preserve that exact operation. For instance, if you take the address of a variable, it suddenly must actually exist (otherwise it could not have an address).

    In other words, don't worry about what the compiler may or may not be doing. The compiler will ensure that the behavior is as you intended. All this under the hood stuff only matters when you go off the reservation in some OTHER way, such as trying to access a variable which has gone out of scope.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21