https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79979
Bug ID: 79979 Summary: For loop fails to exit Product: gcc Version: 4.8.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: jdowdells at hotmail dot com Target Milestone: --- Created attachment 40938 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=40938&action=edit Test code which shows this issue When I compile this code: #include <stdio.h> #include <stdlib.h> #include <time.h> #include <stdio.h> typedef enum { e_0, e_1, e_2, e_3, e_4, e_5, e_6, e_7, e_8, e_9 }EEEE; int dummy_array[8]; int main(void) { int x = dummy_array[8]; EEEE e; printf("X = %d\n", x); printf("address of dummy_array[0] = %p\n", &dummy_array[0]); printf("address of dummy_array[100] = %p\n", &dummy_array[100]); for (e = e_0; e<e_9; e++) { printf("TEST: %d\n", dummy_array[e]); } return 0; } using: gcc -O3 -o test test.c then the for loop never exits ( eventually resulting in seg fault ) If I compile un-opitmised or change: EEEE e; to volatile EEEE e; or to int e; or the array to: int dummy_array[10]; then the loop exits as expected. I know that the array access in the original code is out of bounds and such an access is undefined but we'd expect the for loop to exit even in this circumstance. I tried MSVC and it behaves in an intuitive manner. I've attached a tgz file of my test build with: gcc -v -save-temps -O3 -o test test.c Thanks