Reputation: 4477
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...
Upvotes: 1
Views: 184
Reputation: 985
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.
Upvotes: 3
Reputation: 47104
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.
Upvotes: 5
Reputation: 3172
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).
Upvotes: -2
Reputation: 79991
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.
Upvotes: 0
Reputation: 48577
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.
Upvotes: 1