| Liste des Groupes | Revenir à cl c |
Right. ("for (;;);" in the original program does not.)The idea of all this is given in a footnote in the C standards - "This is intended to allow compiler transformations such as removal of empty loops even when termination cannot be proven."
Note that the C++ special rule applies only when the condition is
equivalent to a constant `true` and the body of the loop is empty.
An implementation can "assume" that any other loop will eventually
finish.
The rule in C is (6.8.6.1p4):
An iteration statement may be assumed by the implementation
to terminate if its controlling expression is not a constant
expression, and none of the following operations are performed
in its body, controlling expression or (in the case of a for
statement) its expression-3
— input/output operations
— accessing a volatile object
— synchronization or atomic operations.
`for (;;)` is treated as having a constant controlling expression.
This covers more cases than the C++ rule.
I dislike it for most of the same reasonss. It should be phrased
in terms of the permitted behavior of a program, not what an
implementation is allowed to "assume".
In addition to that, I dislike the whole idea. I think it's
intended to enable optimizations, but it means that for this
contrived program:
#include <stdio.h>
int main(void) {
bool keep_going = true;
while (keep_going) {
keep_going = true;
}
puts("never reached");
}
the implementation is allowed to "assume" that the loop eventually
terminates. It's not clear what permissions the implementation is being
given if the assumption is violated. I think the program could legally
print "never reached", but if violating the assumption implies undefined
behavior it could do anything.
A programmer could easily write a program similar to the above
and think that the meaning is perfectly clear, have it behave very
differently because of one obscure subclause in the standard.
Les messages affichés proviennent d'usenet.