Are compilers allowed to remove infinite loops like Intel C++ Compiler with -O2 does?
c++, icc, optimization
Solution
Because the `while (q.isEmpty()) ;` loop contains no statements that can ever cause an externally-visible side-effect, the entire loop is being optimised out of existence. It's the same reason that:
for (int i = 0; i < 10; i++)
;
could be optimised out of existence, as long as `i` was not `volatile` (stores to `volatile` objects are part of the "externally visible" effects of a program).
In the C language, it is actually an outstanding bone of contention as to whether an infinite loop is allowed to be optimised away in this manner (I do not know what the situation is with C++). As far as I know, a consensus has never been reached on this issue - smart and knowledgeable people have taken both sides.
Problem
The following testing code does correctly in VS either with debug or release, and also in GCC. It also does correctly for ICC with debug, but not when optimization enabled (`-O2`). ``` #include <cstdio> class tClassA{ public: int m_first, m_last; tClassA() : m_first(0), m_last(0) {} ~tClassA() {} bool isEmpty() const {return (m_first == m_last);} void updateFirst() {m_first = m_first + 1;} void updateLast() {m_last = m_last + 1;} void doSomething() {printf("should not reach here\r\n");} }; int main() { tClassA q; while(true) { while(q.isEmpty()) ; q.doSomething(); } return 1; } ``` It is supposed to stop at `while(q.isEmpty())`. When `-O2` enabled under ICC (release), however, it starts to "doSomething" infinitely. Since this is single-threaded program and `isEmpty()` should be evaluated as `true`, I can find no reason the ICC should behave in this way? Do I miss anything?