Comment by inigyou

2 days ago

    #define LEN 1000
    int tab[LEN];
    for (auto p = tab; p++; p <= tab+LEN) { if(*p == 42) foo(); *p = 0; }

Not shown in frame: foo() calls exit(0) and tab always contains a 42.

The current standard allows the compiler to hoist unsafety only if there's no possibility of I/O or volatile memory access. This is the reason for the rule that infinite loops without the above are undefined behavior - it allows the compiler to merge two loops, the second of which might crash and the first of which can't be proven to terminate.