c - 为什么 GCC 不对无法访问的代码发出警告?

标签 c gcc static-variables gcc-warning

为什么 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 选项 -Wall -Werror应该抛出错误。

最佳答案

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/

相关文章:

python - 更改类中使用的变量?

python - 从类中的静态方法 Python 填充一次静态变量

cygwin 终端将我的字母转换为数字

arrays - 在 C 中实现 memset 以设置整个字而不是逐字节设置

c - 在 linux 集群上为特定用户安装 cmake

编译gcc错误 undefined reference `aes256_init'

c++ - 模板化类的静态变量的多个定义

c - 将输入加载到 C 中的 vector 类型函数时遇到问题

ubuntu - 如何在 Ubuntu (14.04) 上更新 mingw32 中的 gcc?

c - 针对 PowerPC 的 GCC C/C++ 交叉分析实现问题