c - -W 标志,用于 gcc 检测获取未初始化变量的 const 指针

标签 c gcc-warning

我有以下代码,保存在 Coding Ground here 上:

#include <stdio.h>

void foo(int const *x)
{
    printf("Hello, %d!\n", *x);
}

int main()
{
    int y;

    foo(&y);
    y = 3;
    printf("Hello, World %d!\n", y);
    return 0;
}

我编译:

gcc -std=c99 -Wall -Wextra -Wuninitialized -o main *.c

但是,我没有收到有关获取未初始化变量的 const 指针的警告,我 cannot find a suitable flag

注意:将代码粘贴到 Gimpel's Online Lint 会得到预期的结果:

test.c 12 Warning 603: Symbol 'y' (line 10) not initialized

最佳答案

-Wuninitialized Warn whenever an automatic variable is used without first being initialized.

These warnings are possible only in optimizing compilation, because they require data-flow information that is computed only when optimizing. If you don't specify `-O', you simply won't get these warnings.

如果你尝试编译:

gcc -std=c99 -Wall -Wextra -Wuninitialized -O2 -o main *.c

警告将是:

pippo.c: In function ‘main’:
pippo.c:56:5: warning: ‘y’ is used uninitialized in this function [-Wuninitialized]
     printf("Hello, %d!\n", *x);
     ^
pippo.c:61:9: note: ‘y’ was declared here
     int y;
         ^

关于c - -W 标志,用于 gcc 检测获取未初始化变量的 const 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32264463/

相关文章:

c - 为什么我们不必调用C中的main函数?

c - 返回字符串的最后一个单词 - C 编程

c - 数组索引困惑

c - 警告 : integer constant is too large for "long" type

c - gcc std=gnu90 的 size_t 格式警告

c - 我们的练习代码输出了意外的 -1,我们不知道为什么

c - 在 C 中检测 64 位编译

c - 如何消除有关 __FUNCTION__ 的 GCC 迂腐 (-Wpedantic) 警告

c - eclipse C 编译器无法识别//行注释

c++ - GCC 的 -Wpsabi 选项到底有什么作用?压制它有什么影响?