为什么 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 https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wall。我也尝试过-Wunreachable 代码 https://gcc.gnu.org/onlinedocs/gcc-4.4.7/gcc/Warning-Options.html#index-Wunreachable_002dcode-437, -Wextra https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wextra, -迂腐的 https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-pedantic-1, and -ansi https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html#index-ANSI-support(正如所讨论的here https://stackoverflow.com/questions/1730255/gcc-shouldnt-a-warning-be-issued)。然而,这些都没有给我警告。
看起来 GCC 默默地删除了静态变量赋值。
在我看来,GCC 选项-Wall https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wall -Werror https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Werror应该抛出一个错误。
GCC 4.4 会给你一个警告。在 GCC 的更高版本中,此功能(-Wunreachable 代码 https://gcc.gnu.org/onlinedocs/gcc-4.4.7/gcc/Warning-Options.html#index-Wunreachable_002dcode-437) 已被删除。
See 回复:gcc -Wunreachable-code 选项 http://gcc.gnu.org/ml/gcc-help/2011-05/msg00360.html
The -Wunreachable-code
已被删除,因为它不稳定:它
依赖于优化器,因此不同版本的 gcc 会发出警告
关于不同的代码。编译器仍然接受并忽略
命令行选项,这样现有的 Makefile 就不会被破坏。在
未来的某些版本中该选项将被完全删除。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)