开关的情况下不可思议的作用域 [英] Switch case weird scoping
问题描述
回顾一些第三方C code我遇到喜欢的东西就来了:
Reviewing some 3rd party C code I came across something like:
switch (state) {
case 0:
if (c=='A') { // open brace
// code...
break; // brace not closed!
case 1:
// code...
break;
} // close brace!
case 2:
// code...
break;
}
其中在code我正在审查似乎只是一个错字,但我很惊讶,这与出错误编译。
Which in the code I was reviewing appeared to be just a typo but I was surprised that it compiled with out error.
这是为什么合法的C?结果
ç相比,在预期的地方关闭括号什么是对这个$ C $的执行效果?结果
有没有在那里,这可能是使用任何情况下?
Why is this valid C?
What is the effect on the execution of this code compared to closing the brace at the expected place?
Is there any case where this could be of use?
编辑:在我看着都休息的例子是present(如上) - 但答案也可以包括行为,如果万一0打破缺席或1
In the example I looked at all breaks were present (as above) - but answer could also include behaviour if break absent in case 0 or 1.
推荐答案
它不仅是有效的,类似的结构已经在现实code,例如,的达夫设备,这是复制一个缓冲展开循环:
Not only is it valid, similar structure has been used in real code, e.g., Duff's Device, which is an unrolled loop for copying a buffer:
send(to, from, count)
register short *to, *from;
register count;
{
register n = (count + 7) / 8;
switch(count % 8) {
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while(--n > 0);
}
}
由于开关
语句实际上只是计算一个地址并跳转到它,很容易看出为什么它可以与其他控制结构重叠;其他控制结构内的线路有地址,可以跳转目标呢!
Since a switch
statement really just computes an address and jumps to it, it's easy to see why it can overlap with other control structures; the lines within other control structures have addresses that can be jump targets, too!
在这种情况下,你presented,试想如果没有开关
或破
在你的$ C $℃。当您完成执行如果
语句然后
部分,你只要坚持下去,这样你就会崩溃通过到案例2:
。现在,既然你有开关
和破
,它的问题是什么破
能摆脱。按照 MSDN页面的C break语句,
In the case you presented, imagine if there were no switch
or break
s in your code. When you've finished executing the then
portion of a if
statement, you just keep going, so you'd fall through into the case 2:
. Now, since you have the switch
and break
, it matters what break
can break out of. According to the MSDN page, "The C break statement",
在破语句终止最近的封闭的执行做 为 开关或而语句中出现。控制传递给后面的终止语句之后的语句。
The break statement terminates the execution of the nearest enclosing do, for, switch, or while statement in which it appears. Control passes to the statement that follows the terminated statement.
由于最近的封闭的做 为 开关或,而语句是您的开关(请注意,如果不包括在该列表中),那么如果你的然后
块中,转移到在开关
语句之外。更有趣的一点,虽然是如果输入会发生什么 0的情况
,但ç=='A'
是假的。那么如果
控制转移到刚过然后
块的右大括号,你开始执行$ C $下在 2的情况下
。
Since the nearest enclosing do, for, switch, or while statement is your switch (notice that if is not included in that list), then if you're inside the then
block, you transfer to the outside of the switch
statement. What's a bit more interesting, though, is what happens if you enter case 0
, but c == 'A'
is false. Then the if
transfers control to just after the closing brace of the then
block, and you start executing the code in case 2
.
这篇关于开关的情况下不可思议的作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!