本文介绍了为什么GCC不会警告无法访问的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么 gcc(4.6.3)在此示例中没有警告我无法访问的代码:

I wonder why gcc (4.6.3) gives me no warning for the unreachable code in this example:

#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(因为在).但是,这些都没有给我警告.

Note, the purpose of the faulty status() function was to maintain a status. I had expected to get a warning for this with -Wall. I tried also -Wunreachable-code, -Wextra, -pedantic and -ansi (as it was discussed here). Yet, none of those give me a warning.

看来gcc静默删除了静态变量赋值.

It appears gcc silently removes the static variable assignment.

我认为gcc选项-Wall -Werror应该会引发错误.

In my opinion gcc options -Wall -Werror should throw an error.

推荐答案

gcc 4.4会给您警告.在更高版本的gcc中,此功能(-Wunreachable-code)已被删除.

gcc 4.4 will give you warning. In the later versions of gcc this feature (-Wunreachable-code) has been removed.

请参阅此处: http://gcc.gnu.org/ml/gcc- help/2011-05/msg00360.html

伊恩

这篇关于为什么GCC不会警告无法访问的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 03:33
查看更多