-W 标志,用于 gcc 检测获取未初始化变量的 const 指针
-W flag for gcc to detect taking const pointer of uninitialised variable
我有以下代码,保存在 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;
^
为什么这里应该有问题?您只是通过该函数传递了该变量,该函数将指针作为参数。你没有传递它的值,你用 & 传递了它的地址所以编译器不会认为这是错误的,即使你稍后尝试打印它的垃圾值。请记住,您此时打印的是指针 (*x) 的值,而不是变量本身。
如果你不自己做,变量不是用垃圾值初始化的吗?
但是如果你不使用指针:
#include <stdio.h>
void foo(int const x)
{
printf("Hello, %d!\n", x);
}
int main(void){
int y;
foo(y);
y = 3;
printf("Hello, World %d!\n", y);
return 0;
}
现在情况有所不同,编译器知道这一点:
program.c:12:5: error: ‘y’ is used uninitialized in this function [-Werror=uninitialized]
foo(y);
^
cc1: all warnings being treated as errors
我有以下代码,保存在 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;
^
为什么这里应该有问题?您只是通过该函数传递了该变量,该函数将指针作为参数。你没有传递它的值,你用 & 传递了它的地址所以编译器不会认为这是错误的,即使你稍后尝试打印它的垃圾值。请记住,您此时打印的是指针 (*x) 的值,而不是变量本身。 如果你不自己做,变量不是用垃圾值初始化的吗?
但是如果你不使用指针:
#include <stdio.h>
void foo(int const x)
{
printf("Hello, %d!\n", x);
}
int main(void){
int y;
foo(y);
y = 3;
printf("Hello, World %d!\n", y);
return 0;
}
现在情况有所不同,编译器知道这一点:
program.c:12:5: error: ‘y’ is used uninitialized in this function [-Werror=uninitialized] foo(y); ^ cc1: all warnings being treated as errors