Thread: Loop variable

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    Loop variable

    Suppose I declare a loop like this

    Code:
    for(int k = 0;k < 5;k++)
    {
         // some work
    }
    So is the variable 'i local to the loop, that is each time the control enters the loop a new space is allocated for the variable and the space is deallocated after the completion of loop? Is it true for C99 also?

  2. #2
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    All the memory (for a function call) is allocated at once regardless of if the loop is executed or not.
    No the loop does not allocate and deallocate things every itteration. Why? Effiecancy and its would be prety hard to right a compiler to keep track if the variables are allocated or not, because you have goto statements and could do all sorts of things (That may or may not be considered as bad programming practice).

    Edit:
    Just a note: the memory is allocated on the stack enough for all the "auto" variables. However that does not mean the class (If any exist) constructors are called. in most cases I think they are called though.
    Last edited by Benzakhar; 03-26-2007 at 07:08 PM.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Kind of.

    It is true that k goes into scope at the start of the loop, and goes out of scope when the loop finished.

    However, because k is on the Stack, there is absolutely no memory management involved. The Stack is allocated when your program starts, as a big memory space for all local function variables. Stack variables can have constructors and destructor, but besides that no additional memory is allocated. Integers don't have constructors, or destructor, so declaring a local integer is a no-op.

    And yes, you can do this in C++ and C99.
    Last edited by King Mir; 03-26-2007 at 07:16 PM.
    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.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    http://cboard.cprogramming.com/showthread.php?t=85937

    See post #13 of that thread for the relevant information from the C++ standard. If you decide to read the other posts make sure you read the whole thread, as some of the earlier statements were incorrect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop through a 2D array - just for select rows
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 04-17-2009, 08:34 AM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. loop to store values
    By keendoc in forum C++ Programming
    Replies: 1
    Last Post: 10-28-2005, 02:43 PM
  4. for loop not letting me input variable data
    By RealityFusion in forum C++ Programming
    Replies: 6
    Last Post: 09-21-2005, 04:29 PM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM