切换案例奇怪的范围 [英] Switch case weird scoping

查看:12
本文介绍了切换案例奇怪的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看一些第 3 方 C 代码时,我遇到了类似的情况:

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;
}

我正在查看的代码似乎只是一个错字,但令我惊讶的是它编译时没有出现错误.

Which in the code I was reviewing appeared to be just a typo but I was surprised that it compiled with out error.

为什么这是有效的 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?

在示例中,我查看了所有中断(如上) - 但答案也可能包括在情况 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.

推荐答案

不仅有效,在实际代码中也使用了类似的结构,例如,Duff 的设备,这是一个用于复制缓冲区的展开循环:

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);
        }
}

由于 switch 语句实际上只是计算一个地址并跳转到它,因此很容易看出它为什么会与其他控制结构重叠;其他控制结构中的行也有可以作为跳转目标的地址!

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!

在您介绍的情况下,想象一下您的代码中是否没有 switchbreak.当你执行完 if 语句的 then 部分后,你继续前进,所以你会陷入 case 2:.现在,既然你有 switchbreak,那么 break 可以突破什么就很重要了.根据 MSDN 页面,C 中断声明",

In the case you presented, imagine if there were no switch or breaks 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",

break 语句终止最近的封闭 doforswitch 的执行>while 语句.控制权传递给终止语句之后的语句.

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.

因为最近的封闭 doforswitchwhile 语句是您的 switch(注意 if 不包含在该列表中),那么如果您在 then 块内,则转移到 外部switch 语句.不过,更有趣的是,如果您输入 case 0,但 c == 'A' 为 false,会发生什么.然后 if 将控制权转移到 then 块的右大括号之后,然后您开始执行 case 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆