|
-
April 28th, 2004, 01:46 PM
#1
Simple Question about a For Loop
If I have the following:
void SomeFunction()
{
for(int i=0; i<1000; i++)
{
// Do something
}
}
My question is the variable i redeclared as integer for each successive loop?
Is it best to declare i first for stack purposes..
void SomeFunction()
{
int i;
for( i=0; i<1000; i++)
{
// Do something
}
}
I just thinking about better code execution.
-
April 28th, 2004, 01:59 PM
#2
If it were to be declared each successive loop, then your loop would never end cuz the value would always be reset 
it's kinda preference, really. If it's a small function, I'll often times declare it in the for loop. For larger functions, though, where I (may) have more than one loop and would want to re-use the i variable, I declare it at the top of the function with all my other variables
-
April 28th, 2004, 02:02 PM
#3
No it is not redeclared. Only diference that comes to my mind is that (1) will be visible only in the scope of the loop (in VS.NET) in VC6 it will be visible in and after the loop.
-
April 28th, 2004, 02:57 PM
#4
Originally posted by Mr. Tomaszek
Only diference that comes to my mind is that (1) will be visible only in the scope of the loop (in VS.NET) in VC6 it will be visible in and after the loop.
This is not true for the version of VC++.Net that I have (7.1).
Code:
for (int i=0; i<10; i++) {
}
for (i=0; i<10; i++) {
}
This compiles fine.
I believe this is in violation of the ANSI spec though.
-
April 28th, 2004, 03:31 PM
#5
//NOT RELATED TO THE ORIGINAL QUESTION
It is definitely an annoyance
just limit the scope
Code:
{
for (int i=0; i<10; i++)
{
}
}
{
for (int i=0; i<10; i++)
{
}
}
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
April 28th, 2004, 03:34 PM
#6
Originally posted by wayside
This is not true for the version of VC++.Net that I have (7.1).
Code:
for (int i=0; i<10; i++) {
}
for (i=0; i<10; i++) {
}
This compiles fine.
I believe this is in violation of the ANSI spec though.
That would be expected since the inner loop is in the scope of the outer. Where VC deviates from the spec is i is still visible following the outer loop (in your example). It should only exist in the scope of the for loop in which it's declared.
-
April 28th, 2004, 04:09 PM
#7
Originally posted by GCDEF
That would be expected since the inner loop is in the scope of the outer. Where VC deviates from the spec is i is still visible following the outer loop (in your example). It should only exist in the scope of the for loop in which it's declared.
There are no inner or outer loops in this example. If it were, you'd be in trouble modifying loop counter of the outer loop in the inner loop.
ANSI states that the scope of loop variable (declared in the for() statement) is a body of that loop. MS maintains non-standard implementation for backward compatibility with the software already written in non-standard way. There is a compiler switch that makes current Visual C++ compilers compliant with ANSI.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
April 28th, 2004, 04:11 PM
#8
Ah.. didn't know there was a switch. good info
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
April 28th, 2004, 04:53 PM
#9
It would be /Za (or disable lanquage extensions in the project settings)
-
April 28th, 2004, 05:18 PM
#10
Right.
Or, you could specify it like that (MSDN):
/Zc:forScope (Force Conformance in for Loop Scope)
Use /Zc:forScope if you want standard C++ behavior for for loops with Microsoft extensions (/Ze).
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
April 29th, 2004, 07:20 AM
#11
Originally posted by VladimirF
There are no inner or outer loops in this example. If it were, you'd be in trouble modifying loop counter of the outer loop in the inner loop.
ANSI states that the scope of loop variable (declared in the for() statement) is a body of that loop. MS maintains non-standard implementation for backward compatibility with the software already written in non-standard way. There is a compiler switch that makes current Visual C++ compilers compliant with ANSI.
You're right, I misread the brackets. I always find that style of coding very hard to read.
-
April 29th, 2004, 07:43 AM
#12
The fact that non ISO for loop scoping is the default is disappointing. It will encourage people to continue to write non-standard code.
There is a workaround to the for scope problem for VC6 - it limits the scope of variables declared in the for statement to the scope of the loop:
#define for if (0) {} else for
Adrian
-
April 29th, 2004, 10:38 AM
#13
Originally posted by GCDEF
You're right, I misread the brackets. I always find that style of coding very hard to read.
Hehe, I agree with you, but my company's coding standards specify this bracket style, so I've gotten used to it.
-
April 29th, 2004, 11:29 AM
#14
IT IS BETTER TO DECLARE THE VARIABLE INITIALLY.
-
April 29th, 2004, 12:11 PM
#15
Originally posted by S.DARWIN
IT IS BETTER TO DECLARE THE VARIABLE INITIALLY.
WHY???
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|