Liste des Groupes | Revenir à cl c |
bart <bc@freeuk.com> writes:At the language level, the behaviour of FOR loop variable iterating over A to B inclusive, simply has the variable assuming each of those values in turn. It doesn't say how it does that.
[...]The increment is part of its behaviour. At the language level, I couldWhereas for a language like C which is defined by a written standard,
explain how FOR is implemented, but I'd rather not do that.
the standard *must* specify the behavior at the language level
(not in terms of generated intermediate or machine code).
The fact thatI think it's perfectly reasonable for such discussions to assume 'int' is 32 bits. And to only qualify it only when it can matter.
for (int i = INT_MAX-1; i <= INT_MAX; i++);
has undefined behavior can be rigorously inferred from the language
definition.
A note that upper and lower limits must be in the range i64.min + sYou know, of course, that C's int type is not necessarily 32 bits.
.. i64.max - s (where s is the step size) can suffice.
>
What's far more useful is that it naturally works with i64 so has
limits 4 billion times bigger than you get with C's default 'int'
type.
There is no "default" for the width of int. If you had written "with
32-bit int", that would have been clear and correct.
[...]Does it define what value I has when the loop terminates, or is the scope of I local to the loop body?
Maybe the language can detect overflow, but it won't help, as you wantIf you want to implement, say, an Ada-style for loop, detecting
to loop to stop not abort the program. So it is not a solution.
overflow is not an issue at the language level. The issue is that
for the following (I think the Ada syntax is clear enough):
for I in Integer'Last-1 .. Integer'Last loop
// whatever
end loop;
the language semantics specify that the body of the loop must be
executed with I equal to Integer'Last-1, and then with I equal to
Integer'Last. It's the compiler's job to figure out how to do that,
either without triggering an overflow or by handling it cleanly enough
that it doesn't change the behavior.
C's for loop, which is both lower level and more flexible, does notMy post a few hours ago said I'd found a way of getting better-defined behaviour, but I wasn't going to implement it.
impose that particular burden on the compiler. It's up to the
programmer to ensure that a given for loop behaves as desired.
Les messages affichés proviennent d'usenet.