On Sat, Jun 01, 2013 at 11:08:21PM +0200, Jens Bauer wrote: > Here's my result: > > ---8<-----8<-----8<----- > elementTest.c: In function 'main': > elementTest.c:27:19: warning: iteration 8u invokes undefined behavior > [-Waggressive-loop-optimizations] > eightMembers[i] = 0; > ^ > elementTest.c:25:2: note: containing loop > for(i = 0; i < 10; i++) > ^ > --->8----->8----->8----- > > I would expect gcc to complain when it meets the second loop as well as the > third loop, but it didn't detect that there is something wrong with the > second loop. > > ...Is this a bug ?
It is not a bug, the warning isn't guaranteed to report all such cases of undefined behavior, and due to lack of infrastructure in GCC 4.8 can't be reported if certain passes discover the undefined behavior. GCC 4.9 warns on both of the bad loops, but even in 4.9, if the loop doesn't have constant bounds or has multiple exits etc., GCC will not warn and just might optimize based on the fact that undefined behavior doesn't happen in correct code. Lack of warning doesn't mean code is bug free. Jakub