Allocations in Loops

This is a discussion on Allocations in Loops within the C Programming forums, part of the General Programming Boards category; Hi, Simple question I've always wondered about : Program 1: Code: for (int i=0;i<400;i++) { float value = my_array[i]; //do ...

  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
    21,454
    It may be the case that the memory for value is allocated when control enters the function, resulting in no difference either way.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    msh
    msh is offline
    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, 09: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, 12:18 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03: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


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