你說得對。 -Wpointer-arith
應按照documentation給予警告。
我剛纔想下面的程序(故意錯誤):
~/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):如果你給它 '正確' 的代碼
~/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
編譯器並拋出了警告。所以,我會建議你檢查爲什麼它是你所期望的警告。也許你正在編譯的代碼已經改變了?
一個可能的線索,我可以給你:foo = bar + 1;
在上面的代碼觸發警告。但foo = bar ++;
不會(你會得到不同的警告)。所以如果你的代碼在指針上使用增量(或減量)運算符,它可能不會觸發警告。
我知道這不是一個直接的答案,但我希望這可以幫助你專注於你的調查。
您使用的是哪個版本的gcc? – 2010-01-04 06:15:10
你能提供源代碼嗎?通過改進源代碼,您將能夠清除警告。 – Phong 2010-01-04 06:18:45
很抱歉,它基本上是netbsd的一部分..我無法發佈該程序。 gcc版本:arm - netbsdelf-gcc -v使用內置規格。目標:arm - netbsdelf線程模型:posix gcc版本4.1.2 20061021(預發佈)(NetBSD nb3 20061125) – kumar 2010-01-04 06:32:54