c++ - 使用 goto 传递 POD 堆栈变量时的范围和生命周期

标签 c++ c scope goto

我的问题与 C99/GNU-C 和 C++ 中堆栈变量的生命周期有关,当 goto 传递它们时。这里有很多相关的问题,但没有一个真正回答了我的问题。考虑以下代码示例:

void Foo(char *ptr)
{
label1:
    if (ptr)
    {
        char string1[50];
        strcpy(string1, ptr);
        strupr(string1);
        printf("upcased string = %s\n", string1);
        return;
    }

#if CASE_1
    char string2[50] = "test";
#else
    char string2[50];
    strcpy(string2, "test");
#endif
    ptr = string2;
    goto label1;
}

我读到 goto 不会引入新的作用域,因此该变量甚至在声明之前就应该可以访问(理论上)。 string2 存在于函数作用域中,但不能从声明之前的代码中直接访问它。另一方面,使用 goto 和指针变量,可以访问它。 我知道当 goto 向后跨越对象初始化时,C++ 需要调用析构函数,但我没有找到有关内置/POD 类型的生命周期的任何信息。 GCC 测试表明,虽然编译器在 ptr 未分配给 string2 时重用堆栈空间,但当分配完成后,编译器将停止重用它,就好像它“知道”可以在 goto 之后对其进行寻址。

C99/C++ 标准(甚至可能仅限于 GCC)中是否有任何规则明确说明是否允许这样做?我对 C++ 特别感兴趣。

编辑:

  • 处理它的 C++ 标准部分是 “3.7.3-1 显式声明的寄存器或未显式声明的 static 或 extern 的 block 作用域变量具有自动存储期限。这些实体的存储持续到创建它们的 block 退出。” 虽然这似乎证明了上述代码的合理性,但事实并非如此,因为很明显,当编译器知道时,它会重用自动变量的堆栈空间作为优化它将不再被使用。所以需要回答的问题是:即使程序流中带有引用,编译器是否可以假设变量在声明之前的位置没有使用?
  • 我添加了一个替代案例,它似乎有不同的规则。
  • 要回答任何问题,为什么我首先要使用如此丑陋的构造:这当然不是我想要编写正常代码的方式。它应该是兼容性宏的一部分,以允许使用 G++ 的结构化异常处理

最佳答案

关于 C++,C++11 标准第 6.6/1 段规定:

[...] Transfer out of a loop, out of a block, or back past an initialized variable with automatic storage duration involves the destruction of objects with automatic storage duration that are in scope at the point transferred from but not at the point transferred to. [...]

第 3.7.3/3 段随后规定:

If a variable with automatic storage duration has initialization or a destructor with side effects, it shall not be destroyed before the end of its block, nor shall it be eliminated as an optimization even if it appears to be unused, except that a class object or its copy/move may be eliminated as specified in 12.8.

由于string2已初始化,因此程序具有未定义的行为。

也就是说,当您可以使用结构化编程时,为什么要使用 goto 呢? Dijkstra taught us long ago that goto is harmful .

关于c++ - 使用 goto 传递 POD 堆栈变量时的范围和生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16759733/

相关文章:

c++ - 使用 curl 解压 gzip 数据

c - 读取 C 中的重音字符

Javascript fn 在调用时有 2 个参数,但在声明时只有 1 个参数?

AngularJS 隔离范围与集合不起作用

javascript - AngularJS 中 ng-include 内的范围丢失

c++ - 刷新缓存以防止基准测试波动

c++ - 从专用模板方法调用非专用模板方法

c++ - 指针声明中的星号是运算符吗?

c - 在二维数组中搜索一行中的所有零

c - 未初始化的变量/指针成为定义或声明?