1

尝试编译以下代码片段时:

#include <stdio.h>
#include <time.h>

void change_a(int * a) {
    *a = 1;
    return;
}

void main(void) {
    int a = 0;
    change_a(&a);
    if (a) {
        time_t start = time(NULL);
    }
/* do something that never mentions a */

    if (a) {
        time_t end = time(NULL);
       printf("Time spent = %ld\n", (int) end - start);
    }
}

GCC 声明该start变量在以下printf行中未定义:

$ gcc --version
gcc (Debian 4.8.2-1) 4.8.2
[SNIP]
$ gcc -o test.bin test.c 
test.c: In function ‘main’:
test.c:24:44: error: ‘start’ undeclared (first use in this function)
   printf("Time spent = %ld\n", (int) end - start);

另一方面,将 main 函数更改为:

void main(void) {
    int a = 0;
    time_t start = 0;

    change_a(&a);
    if (a) {
        start = time(NULL);
    }
    ...

问题 1

我做错了什么还是编译器做了一些我不知道的有趣的事情?

我认为这可能是编译器太聪明了,优化了那段代码,或者它的启发式方法失败了。但是每隔一次我“发现编译器错误”总是我错过了一些非常明显的错误。

所以在我指责其他聪明人不那么聪明之前,我宁愿聪明的人检查一下。特别是当问题在没有优化的情况下也发生时:

$ gcc -O0 -o test.bin test.c 
test.c: In function ‘main’:
test.c:24:44: error: ‘start’ undeclared (first use in this function)
   printf("Time spent = %ld\n", (int) end - start);
                                            ^
test.c:24:44: note: each undeclared identifier is reported only once for each function it appears in

问题2

我还想知道是否有更好的方法来避免编译器错误(如果不是最后一个代码片段中的解决方法)。如果是我的代码错误(因为响应将包括“正确”代码),这很明显,但我也想知道如何在有人修复 GCC 中的(所谓的)错误时避免错误。

4

2 回答 2

6

在您的第一个示例中,start变量是在语句范围内if声明的。因此,该变量在代码块的末尾(右花括号})超出范围。这绝对不是编译器错误。

您的“解决方法”是正确的解决方案。

有关变量作用域如何在 C/C++(以及许多其他使用 C 风格作用域的语言)中工作的更详尽描述,请参见此处。

于 2013-12-11T22:41:33.157 回答
0

也许“开始”范围在
if (a) { time_t start = time(NULL); }
开始是不能引用 if 块

于 2013-12-11T23:01:30.553 回答