1

This seems like a simple problem, but it is not intuitive to me.

Say you have a loop like this:

int i;
for(i=0;i<10;i++){
float b = 25.2;
float c;
c=b+i;
}

Is there any negative consequences to defining b as float in every single loop? I thought it would have, but I am not so sure because I've seen code that works with this...

Thanks...

O_O
  • 4,397
  • 18
  • 54
  • 69

5 Answers5

5

This is perfectly ok, and in fact I don't think it matters one bit in any decent compiler, if you only use the float inside the loop.

It does make sense for code clarity to put it in the loop, but it's a matter of taste mostly.

But beware for situations like

int i,j;
for ( i=0;i<count;i++ )
{
    int j;
    // stuff
}

I've seen similar situations not generate compiler warnings, which makes for hard to trace bugs.

edit just tested, gcc does compile differently, but with -O3 the generated assembly is identical. Test with gcc -S file.c. Update: -O1 is enough, and it actually depends on the order you declare variables. If the float was declared below int i; in your example, the compiled assembly will still be identical.

mvds
  • 45,755
  • 8
  • 102
  • 111
  • @O_O: Optimization levels in GCC, `-O1` through `-O3` (that's the letter O, not the number 0) tell the compiler to optimize the code from "a little bit" to "quite a lot." `-Os` instructs the compiler to optimize for code size. See http://stackoverflow.com/questions/1778538/how-many-gcc-optimization-levels-are-there – nmichaels Jan 06 '11 at 23:16
  • Those are optimization flags. The higher the number, the more tricks. But the tricks may also have downsides: Optimized assembly may be faster, but bigger as well, and debugging (highly) optimized code can be harder, since the 1:1 relationship with the C code is further away. – mvds Jan 06 '11 at 23:20
3

On pre-C99 compilers, the same stack space would often be used for each iteration, so there actually was no performance impact for declaring a variable in a loop, since it was not necessary to allocate more stack space during successive iterations. I imagine that C99 compilers do something similar, but I don't know for sure.

Most compilers will just optimize this away. The worst case is that you're creating a new float on the stack, which is an extremely inexpensive operation on most architectures. There may be cases where it is even be faster to declare the variable immediately before use. Though, if your program is that performance sensitive, you should probably be using assembly language to begin with.

C99 and later specify that variables are scoped to the loop that they are created in, while earlier versions of C are ambiguous. Different compilers implement it differently. This is important to be aware of because you may run into naming conflicts on pre C99 compilers if a variable of the same name exists in the scope of the function containing the loop (since they will treat the variable as being scoped to the function).

Personally, I declare variables in loops all the time. It performs well and clearly indicates that the variables exist to be used in that loop. It's a matter of structuring your code in a way that clearly indicates the intentions of that code.

overstood
  • 985
  • 6
  • 7
1

Assuming the compiler doesn't optimize here, there is a small penalty paid in allocating the float on every loop. It would probably be better to declare the float outside of the loop.

It depends on the code though. Sometimes it's cleaner to declare the variable like that. The price you pay in performance is probably pretty small.

Falmarri
  • 47,727
  • 41
  • 151
  • 191
0

You don't initialize b, so you're invoking undefined behavior by using it.

If you're worried about performance issues, the compiler might just optimize the allocation away.

wkl
  • 77,184
  • 16
  • 165
  • 176
-2

If you define a variable in a block (stuff enclosed by braces) it just restricts its scope, you can't use it outside. It just makes the program cleaner.

The program may allocate your variables on stack at the opening bracet and dropping at the closing bracet, but a good compiler should do it on the point of entering and leaving the subroutine (method).

Also, it's a C++ feature, and really a step forth to well-structured programming (and also it makes the C++ programming a bit like using script languages, where you don't need to declare variables, but don't tell this to serious C or Java programmers).

ern0
  • 3,074
  • 25
  • 40
  • As far as I know, declaring a variable at the start of any block is a C feature, not specifically C++. – mvds Jan 06 '11 at 22:54
  • C++ allows arbitrary declaration of new variables at any point in a block, while strict C89 only allowed it in the beginning of a block. That does not apply to this example. C99 also allows the same variable declaration behavior as C++, and this behavior has been part of compiler extensions like in GNU gcc for a while. – wkl Jan 06 '11 at 23:00
  • I remember old times, when all the local variables was required to be listed before any code. Maybe, it was an earlier C standard. (Or maybe I remember wrong. I'm very old!) Hm, it says that in C, declarations must be at the beginning of a *block*, not function: http://docs.hp.com/en/92501-90029/ch01s03.html#d0e837 Yep, probably you're right. – ern0 Jan 06 '11 at 23:04
  • 1
    Depending on the C standard that your using, variables declared in loops may be available after the loop (and even cause naming conflicts!). C99 or later specifies that variables declared in a loop are scoped to that loop. It's ambiguous in earlier versions, and is implemented differently between compilers. – overstood Jan 07 '11 at 01:29