尝试编译以下代码片段时:
#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 中的(所谓的)错误时避免错误。