This is due to undefined behavior, you are accessing the array mc
out of bounds on the last iteration of your loop. Some compilers may perform aggressive loop optimization around the assumptions of no undefined behavior. The logic would be similar to the following:
- Accessing
mc
out of bounds is undefined behavior
- Assume no undefined behavior
- Therefore
di < 4
is always true since otherwise mc[di]
would invoke undefined behavior
gcc with optimization turned on and using the -fno-aggressive-loop-optimizations
flag causes the infinite loop behavior to disappear(see it live). While a live example with optimization but without -fno-aggressive-loop-optimizations exhibits the infinite loop behavior you observe.
A godbolt live example of the code shows the di < 4
check is removed and replaced with and unconditional jmp:
jmp .L6
This is almost identical to the case outlined in GCC pre-4.8 Breaks Broken SPEC 2006 Benchmarks. The comments to this article are excellent and well worth the read. It notes that clang caught the case in the article using -fsanitize=undefined
which I can not reproduce for this case but gcc using -fsanitize=undefined
does (see it live). Probably the most infamous bug around an optimizer making an inference around undefined behavior is the Linux kernel null pointer check removal.
Although this is an aggressive optimizations, it is important to note that as the C++ standard says undefined behavior is:
behavior for which this International Standard imposes no requirements
Which essentially means anything is possible and it notes (emphasis mine):
[...]Permissible undefined behavior
ranges from ignoring the situation completely with unpredictable results, to behaving during translation or
program execution in a documented manner characteristic of the environment (with or without the issuance of
a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).[...]
In order to get a warning from gcc we need to move the cout
outside the loop and then we see the following warning (see it live):
warning: iteration 3u invokes undefined behavior [-Waggressive-loop-optimizations]
for(di=0; di<4;di++,delta=mc[di]){ }
^
which would have likely been sufficient to provide the OP with enough information to figure out what was going on. Inconsistency like this are typical of the types of behavior we can see with undefined behavior. To get a better understanding of why such waring can be inconsitent in the face of undefined behavior Why can't you warn when optimizing based on undefined behavior? is a good read.
Note, -fno-aggressive-loop-optimizations
is documented in the gcc 4.8 release notes.
cout << di
is probably that the stream insertion operator for complex passes the address ofdi
to some "opaque" code (or that the stream insertion operator for complex is opaque itself - which would surprise me though). And depending on what that "opaque" code does, the behavior of the program could still be well defined. I'm not saying that it would be impossible to provide a warning in this case without too many false positives (or even any false positives). Only that it would be quite hard. – Paul Groke