为什么 GCC (4.6.3) 在下面的例子中没有给我任何关于无法访问的代码的警告?
#include <stdio.h>
int status(void)
{
static int first_time = 1;
if (first_time) {
return 1;
first_time = 0; /* Never reached */
} else {
return 0;
}
}
int main(int argc, const char *argv[])
{
printf("first call %d\n", status());
printf("second call %d\n", status());
return 0;
}
请注意,错误的 status()
函数的目的是维持状态。我曾期望通过 -Wall 收到警告.我也试过-Wunreachable-code , -Wextra , -pedantic , 和 -ansi (正如讨论的那样 here )。然而,这些都没有给我警告。
GCC 似乎默默地删除了静态变量赋值。
最佳答案
GCC 4.4 会给你一个警告。在 GCC 的更高版本中,此功能 (-Wunreachable-code) 已被删除。
参见 Re: gcc -Wunreachable-code option
The
-Wunreachable-code
has been removed, because it was unstable: it relied on the optimizer, and so different versions of gcc would warn about different code. The compiler still accepts and ignores the command line option so that existing Makefiles are not broken. In some future release the option will be removed entirely.
关于c - 为什么 GCC 不对无法访问的代码发出警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17249934/