gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:57:27 24 4
gpt4 key购买 nike

我有以下代码,保存在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com