Is an infinite loop like for (;;);
undefined behavior in C? (It is for C++, but I don't know about C.)
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- How to stop infinite loop in JShell / Kulla?
- Index of single bit in long integer (in C) [duplic
The rationale for this question with relevance to C++ isn't relevant to C. Section 5.1.2.3p6 states the limits to optimisation, and one of them is:
Now the question becomes "What data would execution according to the abstract semantics have produced?". Assuming a signal interrupts the loop, the program may very well terminate. The abstract semantics would have produced no output prior to that signal being raised, however. If anything, the compiler may optimise the
puts("Hello");
away.No, the behavior of a
for (;;)
statement is well defined in C.N1570, which is essentially identical to the offical 2011 ISO C standard, says, in section 6.8.5 paragraph 6:
with two footnotes:
The first footnote makes it clear that
for (;;)
is treated as if it had a constant controlling expression.The point of the rule is to permit optimizations when the compiler can't prove that the loop terminates. But if the controlling expression is constant, the compiler can trivially prove that the loop does or does not terminate, so the additional permission isn't needed.