Thread: declaring variables inside loops

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    And the only problem I have is that if you declare an initialized variable inside the for loop, it will be initialized in every single loop. So... its not good in some cases.
    The variable will be reinitialized for each loop if you indicated to do so (number=0 below), this is
    not the same problem than reallocating it and except if you turn on the optimizer, there is nothing
    the compiler can (should) do here.

  2. #17
    abyss - deep C
    Join Date
    Oct 2007
    Posts
    46
    how about declaring it
    Code:
    static int number = 0;
    I guess (not sure) it would not initialize it each time it enters the loop. Somebody please confirm :-)

    maverix

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by maverix View Post
    how about declaring it
    Code:
    static int number = 0;
    I guess (not sure) it would not initialize it each time it enters the loop. Somebody please confirm :-)

    maverix
    Correct, it will not initialize static variables for each loop iteration (or for every call to a function for example). The compiler MAY, however, generate less good code with static variables, since there is (theoretically) a possibility that they are modified outside of the function without the compiler knowing about it. I haven't studied that much - but essentially, a static variable is a global. However, since it's limited scope, I suppose the compiler could know if the address of the static is ever exported out of the function, and thus whether it is susceptible to modification outside of the function or not.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Some compiler optimizers might not be as good as others, and in debug mode it won't optimize anything.
    I use MinGW, how do I know if it is in debug mode? I don't use (or know how to use) a debugger, so I want to make sure that they are out of debug mode for now.

    how about declaring it

    Code:
    static int number = 0;
    I guess (not sure) it would not initialize it each time it enters the loop. Somebody please confirm :-)

    maverix
    Correct, it will not initialize static variables for each loop iteration (or for every call to a function for example).
    I supposed that the same goes for an if block right? Every time it enters the if, it won't reinitialize if I declare it static?

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    gcc (which is the actual compiler part of MingW) needs the switch -O to optimize, and -O2, -O3 and -O4 will make the optimization increasingly more "aggressive" (and sometimes means that code either won't work, or actually run slower). Debug mode (-g in gcc) is actually not really affecting anything other than generation of information that the debugger (gdb) can use to figure out where in the code you are executing and where to find the variables and such.

    Static variables are initialized (to zero if nothing else is given as initialization) when the executable file is loaded, that's it - never ever again.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    gcc Optimization Options
    -O3 is the highest one I see.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    gcc Optimization Options
    -O3 is the highest one I see.
    Yes, I think -O4 is actually equal to -O3 on older compilers - but gcc has moved on from 3.2.3 you know. The latest version is 4.4 or so (sorry, too lazy to look up the current latest version).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  2. Declaring SDL_Rect variables in a class?
    By pwnz32 in forum C++ Programming
    Replies: 10
    Last Post: 07-27-2008, 07:12 PM
  3. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM
  4. simple question about variables and loops
    By InvariantLoop in forum C Programming
    Replies: 2
    Last Post: 01-26-2005, 09:47 AM
  5. static variables inside of a class.. possible?
    By revelation437 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2003, 05:21 PM