简体   繁体   中英

c gcc compiler options for pointer arithmetic warning

I'm using the following flags, but still I m not able to get this warning:

pointer of type void * used in arithmetic

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 should catch this type of warning, but I'm not able to get this warning:

pointer of type void * used in arithmetic

Which specific cflag should be used to get this warning?

Edit: my mistake, it is there as part of a macro check which is not defined. :( By defining that macro, I'm able to get that error.

With gcc 4.2.1 on OS X, I get this warning:

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

for the following program:

#include <stdio.h>

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

I am compiling it as:

$ gcc -Wpointer-arith p.c

Can you post your program, or post the result of compiling the above?

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;
}

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

and

~/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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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