繁体   English   中英

用于指针算术警告的 c gcc 编译器选项

[英]c gcc compiler options for pointer arithmetic warning

我正在使用以下标志,但仍然无法收到此警告:

用于算术的void *类型指针

使用的标志:

-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应该捕获这种类型的警告,但我无法收到此警告:

用于算术的void *类型指针

应该使用哪个特定的 cflag 来获得此警告?

编辑:我的错误,它是未定义的宏检查的一部分。 :( 通过定义那个宏,我可以得到那个错误。

在 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

你能发布你的程序,或者发布上面编译的结果吗?

你是对的。 -Wpointer-arith应该根据文档给你一个警告。

我刚刚尝试了以下程序(故意出错):

~/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 ++; 不会(您会收到不同的警告)。 因此,如果您的代码在指针上使用递增(或递减)运算符,则可能不会触发警告。

我知道这不是一个直接的答案,但我希望这可以帮助您专注于调查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM