Thread: Allocations in Loops

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    19

    Allocations in Loops

    Hi,

    Simple question I've always wondered about:

    Program 1:
    Code:
    for (int i=0;i<400;i++) {
        float value = my_array[i];
        //do something with value
    }
    Program 2:
    Code:
    float value;
    for (int i=0;i<400;i++) {
        value = my_array[i];
        //do something with value
    }
    Which is more efficient? In the first program, is "value" allocated and deallocated each loop iteration?

    Thanks,
    Ian

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It may be the case that the memory for value is allocated when control enters the function, resulting in no difference either way.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    12
    Quote Originally Posted by Geometrian View Post
    Which is more efficient? In the first program, is "value" allocated and deallocated each loop iteration?
    No, the variable space is allocated only once the before/at the start of the loop, the only difference is that any variable you may declare inside a block, cannot be used outside of the block.

    In Program 1, it is illegal to inspect and/or modify the variable 'value' after the loop finishes. In Program 2, it's perfectly fine to do so.

    On a side note: according to the C90 standard, you are only allowed to declare new variables at the start of any block. Unlike C++, where you can mix code and declarations freely everywhere.

    LT

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Lord Thunder View Post
    On a side note: according to the C90 standard, you are only allowed to declare new variables at the start of any block. Unlike C++, where you can mix code and declarations freely everywhere.
    For completeness sake then, it should be noted that C99 allows declaring variables anywhere in the code, as long as it is before the first use.

    Whether "can" implies "should" is debatable. I've come to a conclusion that I prefer to keep mine at the start of the block. It makes for a cleaner-looking code.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays and loops
    By pmooney12 in forum C Programming
    Replies: 2
    Last Post: 11-22-2010, 10:38 PM
  2. loops, menu loops
    By gloworm in forum C Programming
    Replies: 17
    Last Post: 04-12-2010, 07:59 PM
  3. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM

Tags for this Thread