c gcc编译器选项用于指针算术警告 [英] c gcc compiler options for pointer arithmetic warning
问题描述
我使用以下标志,但仍然我不能得到这个警告:
类型void *的指针使用在算术
I m using the following flags , but still I m not able to get this warning: "pointer of type 'void *' used in arithmetic"
使用的标志:
-O2 -Werror -Wall -Wno-main -Wno-format-zero-length -Wpointer-arith -Wmissing-prototypes -Wstrict-prototypes -Wswitch -Wshadow -Wcast-qual- strings -Wno-sign-compare -Wno-pointer-sign -Wno-attributes -fno-strict-aliasing
Flags used: -O2 -Werror -Wall -Wno-main -Wno-format-zero-length -Wpointer-arith -Wmissing-prototypes -Wstrict-prototypes -Wswitch -Wshadow -Wcast-qual -Wwrite-strings -Wno-sign-compare -Wno-pointer-sign -Wno-attributes -fno-strict-aliasing
-Wpointer-arith应该捕获这种类型的警告,
-Wpointer-arith should catch this type of warning, but I m not able to get this warning."pointer of type 'void *' used in arithmetic"
应该使用哪个特定的cflag来获取这个警告?
Which specific cflag should be used to get this warning?
========================== ============
==================================================
推荐答案
You're right. -Wpointer-arith
should give you a warning as per the documentation.
我刚刚试过下面的程序(有意错误):
I have just tried the following program (with intentional error):
~/code/samples$ cat foo.c
#include <stdio.h>
int main (int argc, char **argv)
{
void * bar;
void * foo;
foo = bar + 1;
return 0;
}
我只用 -Wpointer-arith
选项,以及上面列出的所有选项两个尝试都提出了所需的警告。我使用gcc版本4.3.4(Debian 4.3.4-6):
I have compiled the program with just the -Wpointer-arith
option, and all your options as listed above. Both attempts threw up the desired warning. I am using gcc version 4.3.4 (Debian 4.3.4-6).:
~/code/samples$ gcc -Wpointer-arith foo.c
foo.c: In function ‘main’:
foo.c:6: warning: pointer of type ‘void *’ used in arithmetic
和
~/code/samples$ gcc -O2 -Werror -Wall -Wno-main -Wno-format-zero-length -Wpointer-arith -Wmissing-prototypes -Wstrict-prototypes -Wswitch -Wshadow -Wcast-qual -Wwrite-strings -Wno-sign-compare -Wno-pointer-sign -Wno-attributes -fno-strict-aliasing foo.c
cc1: warnings being treated as errors
foo.c: In function ‘main’:
foo.c:6: error: pointer of type ‘void *’ used in arithmetic
如果你给它'正确的'代码,编译器会抛出警告。因此,我建议您检查 为什么 ,您会收到此警告。也许编译的代码已经改变了?
The compiler does throw up the warning if you give it the 'right' code. So, I would recommend you examine why it is you expect this warning. Maybe the code you're compiling has changed?
一个可能的线索我可以给你: foo = bar + 1;
在上面的代码中触发警告。但 foo = bar ++;
不会(你得到一个不同的警告)。因此,如果你的代码对指针使用了增量(或递减)运算符,它可能不会触发警告。
One possible clue I can give you: foo = bar + 1;
in the code above triggers the warning. But foo = bar ++;
will not (You get a different warning). So if your code uses increment (or decrement) operators on pointers, it will probably not trigger the warning.
我知道这不是一个直接的答案,可帮助您集中调查。
I know this is not a direct answer, but I hope this helps you focus your investigation.
这篇关于c gcc编译器选项用于指针算术警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!