c代表指针运算警告gcc编译器选项 [英] c gcc compiler options for pointer arithmetic warning

查看:538
本文介绍了c代表指针运算警告gcc编译器选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我米使用下面的标志,但还是我不是能够得到这样的警告:
指针类型的算术使用'无效*'。

I m using the following flags , but still I m not able to get this warning: "pointer of type 'void *' used in arithmetic"

标志用于:
-O2 -Wall -Werror -Wno-主要-Wno格式的零长度-Wpointer-ARITH -Wmissing的原型-Wstrict的原型-Wswitch -Wshadow -Wcast-QUAL -Wwrite弦-Wno-sign-compare的-Wno终场符号-Wno-属性-fno严格走样

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?

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

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

推荐答案

您说得对。 -Wpointer-ARITH 应该给你一个警告,因为按照的文档

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

的编译器,如果你给它'正确'code扔了警告。所以,我建议你检查 为什么这是你想到这个警告。也许你正在编译code发生了变化?

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?

一个可能的线索,我可以给你:富=栏+ 1; 在上面的code触发警告。但富=酒吧++; 不会(你会看到一个不同的警告)。所以,如果你的code使用递增(或递减)运算符的指针,它可能不会触发警告。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆