gpt4 book ai didi

c - 使用 goto 跳转到内部或兄弟范围

转载 作者:太空狗 更新时间:2023-10-29 16:43:48 24 4
gpt4 key购买 nike

是否允许跳转到内部范围或兄弟范围内的标签?如果是这样,是否允许使用在该范围内声明的变量?

考虑这段代码:

int cond(void);
void use(int);

void foo()
{
{
int y = 2;
label:
use(y);
}

{
int z = 3;
use(z);

/* jump to sibling scope: */ if(cond()) goto label;
}

/* jump to inner scope: */ if(cond()) goto label;
}

这些 goto 合法吗?

如果是这样,当我跳转到 label 并保留分配给它的最后一个值 (2) 时,y 是否保证存在?

或者是否允许编译器假设 y 在超出范围后不会被使用,这意味着单个内存位置可以同时用于 yz?

如果此代码的行为未定义,我如何让 GCC 发出警告?

最佳答案

来自 C99 标准(强调我的):

6.2.4 Storage durations of objects

[6] For such an object that does have a variable length array type, its lifetime extends from the declaration of the object until execution of the program leaves the scope of the declaration. ... If the scope is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate.

6.8.6.1 The goto statement

[1] The identifier in a goto statement shall name a label located somewhere in the enclosing function. A goto statement shall not jump from outside the scope of an identifier having a variably modified type to inside the scope of that identifier.

[4] ... A goto statement is not allowed to jump past any declarations of objects with variably modified types.

结论

  1. y 不是可变修改类型,因此根据标准,跳转是合法

  2. y 保证存在,但是,跳转会跳过初始化(y = 2),所以 y 的值> 是不确定的

  3. 您可以使用 -Wjump-misses-init 让 GCC 发出如下所示的警告:

    warning: jump skips variable initialization [-Wjump-misses-init]


在C++中,跳转是不合法的,C++不允许跳过y的初始化。

关于c - 使用 goto 跳转到内部或兄弟范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37371794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com