Eric Smith [eric at
brouhaha.com] wrote:
If the compiler optimized that out entirely, the
compiler is
defective. Optimizations are not supposed to change the semantics of
well-formed code.
On the other hand, the compiler is free to optimize
int i;
for (i = 0; i < 100; i++)
;
into
int i;
i = 100;
Since that does not change the semantics.
Indeed. In fact the very first time I helped someone with a "program
bug"
was precisely that example, except in FORTRAN (on VAX/VMS). He wanted
to slow down execution of the program for some reason I forget now and
found that it ran at the same speed no matter how large a value of 100
he chose.
He wouldn't believe that the FORTRAN compiler had helpfully turned the
loop
into a simple assignment until I showed him exactly what it had done in
the listing file.
Antonio