Xcode "infinite loop" inconsistency
When running this code under debug in the iPhone simulator, it correctly exits with r==4 after 5 iterations of the loop:-
int r;
for (r = 0;;r++)
{
if (r == 4)
break;
}
Without the braces, it exits with r==1 after 1 iteration:-
int r;
for (r = 0;;r++)
if (r == 4)
break;
Without the braces, but with a test expression which is true, in place of the permanently true default for a missing test expression, it correctly exits with r==4 after 5 iteratio开发者_如何学编程ns of the loop:-
int r;
for (r = 0; r > -100;r++)
if (r == 4)
break;
Am I missing something here, or is the second example a compiler bug?
I don't have the simulator handy, but if that's really what's happening, then yes, something is going wrong. Your first and second example are identical as far as the language is concerned. Are you quite sure your tests are accurate? That the code you're showing us above is exactly what you're running in the simulator?
Edit: OK I tried it, and it works correctly in the simulator here. My best guess for a solution to your problem is that you have:
for (r = 0;r++;)
By accident in the case you think is failing. Note the empty loop statement is moved from what you wrote above. This for loop would give you the result of 1.
Edit 2: After reading some other answers here, I thought I should check out the debugger. Take a look at this screenshot! image http://img38.imageshack.us/img38/7128/xcodeh.png
If I press "step over" again, it goes back into the loop though; just a little graphical hiccup.
I don't know if your question should be considered as duplicate but maybe this is the answer:
Are loops with and without parenthesis handled differently in C?
The debugger executes one statement at a time.
I was a bit mistaken in my previous answer. Of course the debugger also executes the loop 4 times. The difference is , that it looks like he exits after one iteration. When I debug the following code:
#include <stdio.h>
int main (int argc, const char * argv[]) {
int r;
for (r = 0;;r++)
if (r == 4)
break;
printf("%i",r);
return 0;
}
the debugger processes the for
line, then the if
line, the for
line again and then highlights the printf
line. So it looks like printf
is going to be executed but in the next step, the debugger highlights the if
line again, then going to for
, thereby increasing r
and then highlighting printf
again.
This is the code I tried in Xcode and it is basically the same behavior as described above (I just added some lines of code in main.m
in a fresh Cocoa application project):
#import <Cocoa/Cocoa.h>
int main(int argc, char *argv[])
{
int r;
for (r = 0;;r++)
if (r == 4)
break;
return NSApplicationMain(argc, (const char **) argv);
}
Picture with Cocoa application:
Debugger http://img691.imageshack.us/img691/3844/1002060002.png
So the question is, have you really clicked through the loop step by step and is the loop really exiting after one iteration, or have you just stopped after one iteration?
Looks like a compiler bug to me. What compiler do you have set to, gcc or llvm-gcc?
I just tested this with gcc 4.2.1 (Apple Inc. build 5646) and get the right result in both cases.
That's not a bug. That's Just basically C syntax. Without brackets a for/while loop or an if condition are considered Just one line after the declaration, what you find in the next line doesn't belong to the loop/condition anymore. Otherwise having the brackets you actually define as lines as you what within your iteration. Hope this helps.
精彩评论