Xcode“无限循环”不一致

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2211456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:06:45  来源:igfitidea点击:

Xcode "infinite loop" inconsistency

objective-cxcodefor-loop

提问by user258279

When running this code under debug in the iPhone simulator, it correctly exits with r==4 after 5 iterations of the loop:-

在 iPhone 模拟器中调试下运行此代码时,它在循环的 5 次迭代后以 r==4 正确退出:-

int r;
for (r = 0;;r++)
{
    if (r == 4)
        break;
}

Without the braces, it exits with r==1 after 1 iteration:-

没有大括号,它在 1 次迭代后以 r==1 退出:-

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 iterations of the loop:-

没有大括号,但测试表达式为真,代替缺失测试表达式的永久真默认值,它在循环的 5 次迭代后正确退出 r==4:-

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?

我在这里遗漏了什么,还是第二个示例是编译器错误?

回答by Carl Norum

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.

偶然在你认为失败的情况下。请注意,空循环语句是从您上面写的内容中移出的。这个 for 循环会给你 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

编辑 2:在这里阅读了其他一些答案后,我想我应该检查一下调试器。看看这个截图! 图片 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.

如果我再次按下“step over”,它会回到循环中;只是一个小的图形打嗝。

回答by Felix Kling

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?

带括号和不带括号的循环在 C 中的处理方式不同吗?

The debugger executes one statementat 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 lookslike he exits after one iteration. When I debug the following code:

我之前的回答有点误会。当然,调试器也会执行 4 次循环。不同的是,看起来他在一次迭代后就退出了。当我调试以下代码时:

#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 forline, then the ifline, the forline again and then highlights the printfline. So it looks like printfis going to be executed but in the next step, the debugger highlights the ifline again, then going to for, thereby increasing rand then highlighting printfagain.

调试器处理该for线路,则该if线路中,for再次行,然后凸显printf线。所以看起来printf将要执行,但在下一步中,调试器if再次突出显示该行,然后转到for,从而增加r然后printf再次突出显示。

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.min a fresh Cocoa application project):

这是我在 Xcode 中尝试的代码,它的行为与上述基本相同(我只是main.m在新的 Cocoa 应用程序项目中添加了一些代码行):

#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:

使用 Cocoa 应用程序的图片:

Debugger http://img691.imageshack.us/img691/3844/1002060002.png

调试器 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?

所以问题是,你是否真的一步一步地点击了循环,循环是否真的在一次迭代后退出,或者你在一次迭代后就停止了?

回答by DennyLou

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.

那不是错误。那只是基本上 C 语法。如果没有括号,则认为 for/while 循环或 if 条件在声明后仅一行,您在下一行中找到的内容不再属于循环/条件。否则,您实际上将括号定义为迭代中的行。希望这可以帮助。

回答by AlBlue

Looks like a compiler bug to me. What compiler do you have set to, gcc or llvm-gcc?

对我来说看起来像是编译器错误。你设置了什么编译器,gcc 还是 llvm-gcc?

I just tested this with gcc 4.2.1 (Apple Inc. build 5646) and get the right result in both cases.

我刚刚用 gcc 4.2.1 (Apple Inc. build 5646) 对此进行了测试,并在两种情况下都得到了正确的结果。