2010-01-04 104 views
1

我米使用下面的標誌,但還是我不是能夠得到這樣的警告GCC編譯器選項: 「型算術使用‘無效*’的指針」下指針運算警告

標誌使用: -O2 -Werror -Wall -Wno-main -Wno-format-zero-length -Winterinter-arith -Wmissing-prototypes -Wstrict-prototypes -Wswitch -Wshadow -Wcast-qual -Wwrite-strings -Wno-sign-compare -Wno -pointer-sign -Wno-attributes -fno-strict-aliasing

-Winterinter-arith應該捕獲這種類型的警告,但我不能得到這個警告。「在算術中使用的類型爲'void *'的指針「

應該使用哪個特定的cflag來獲取此警告?

============================================== ====

我的錯誤,它是作爲未定義的宏檢查的一部分。 。:(通過定義宏,我爲能夠得到這個錯誤

+0

您使用的是哪個版本的gcc? – 2010-01-04 06:15:10

+0

你能提供源代碼嗎?通過改進源代碼,您將能夠清除警告。 – Phong 2010-01-04 06:18:45

+0

很抱歉,它基本上是netbsd的一部分..我無法發佈該程序。 gcc版本:arm - netbsdelf-gcc -v使用內置規格。目標:arm - netbsdelf線程模型:posix gcc版本4.1.2 20061021(預發佈)(NetBSD nb3 20061125) – kumar 2010-01-04 06:32:54

回答

2

你說得對。 -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 ++;不會(你會得到不同的警告)。所以如果你的代碼在指針上使用增量(或減量)運算符,它可能不會觸發警告。

我知道這不是一個直接的答案,但我希望這可以幫助你專注於你的調查。

+0

)我們真正想要的是使用「+」的任何指針運算的警告。並認爲我找到了答案。但指針運算是C的靈魂的一部分。當然,cerr <<「PTR」<<(「Yipes」+ 1);已驗證。 – Tuntable 2016-03-30 06:02:43

2

隨着OS X的gcc 4.2.1,我得到這樣的警告:

p.c:7: warning: wrong type argument to increment 

以下程序:

#include <stdio.h> 

int main(void) 
{ 
    int i[] = { 42 }; 
    void *p = i; 
    printf("%p\n", p++); 
    return 0; 
} 

我編譯它爲:?

$ gcc -Wpointer-arith p.c 

你可以發佈你的程序,或張貼編譯上述結果

+0

對不起..its基本上是netbsd的一部分..我無法發佈程序。 gcc版本: 手臂 - netbsdelf-GCC -v 使用內置的規格 目標:手臂 - netbsdelf 線程模型:POSIX gcc版本4.1.2 20061021(搶鮮)(NetBSD的NB3 20061125) – kumar 2010-01-04 06:31:42

+0

我錯誤,它是作爲未定義的宏檢查的一部分。 :( – kumar 2010-01-04 07:21:48