Reputation: 41
I'm building a fairly complex application, which built without any errors/warnings with -O0
. However when I tried -O3
, I got a couple of them which are puzzling. For example:
1: static pinfo_t* pinfo[2] = { &gv1, &gv2 }; // GLOBAL
2:
3: int i;
4: int x = sizeof(pinfo);
5:
6: for (i = 0; i < x; i ++)
7: if (pinfo[i]->id == NO_ID)
8: printf("%s\n", pinfo[i]->name);
Note the compiler (gcc v4.3.2) built this successfully with -O0
(with O1 and O2 also). But with -O3
, the compiler correctly pinpoints line 7 to be a potential problem causing line with the error:
error: array subscript is above array bounds
Fair enough, but when I comment out line 7, it has no problem with line 8, which should also have been flagged!
Any help is appreciated!
Upvotes: 3
Views: 804
Reputation: 10613
Dollars to donuts that the compiler completely unrolls the loop at -O3 to something like:
...
if (pinfo[0]->id == NO_ID)
printf("%s\n", pinfo[0]->name);
if (pinfo[1]->id == NO_ID)
printf("%s\n", pinfo[1]->name);
if (pinfo[2]->id == NO_ID)
printf("%s\n", pinfo[2]->name);
...
It then observes that the generated index is out of bounds and warns you about it.
Upvotes: 2
Reputation: 37188
Wrt the warnings and optimization level, in GCC some warnings are generated when analyzing the code during optimization passes, and thus when those passes are not enabled, those warnings are not emitted either. It's a long-standing issue, but so far it hasn't propagated high enough on anyone's TODO list.
Upvotes: 8