最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

freepascal - value of iteration variable after FOR loops in FPC - Stack Overflow

programmeradmin3浏览0评论

When working with FPC 3.2.3 (Lazarus 4.0 RC1) I noticed that the variable handling in for loops does not work as expected:

for i:=0 to 10 do Log(IntToStr(i));
Log('@end:'+IntToStr(i));

returns:

(compiler optimization level 0):   0 1 2 3 4 5 6 7 8 9 10 @end:10
(compiler optimization level 1):   0 1 2 3 4 5 6 7 8 9 10 @end:10
(compiler optimization level 2):   0 1 2 3 4 5 6 7 8 9 10 @end:10
(compiler optimization level 3):   0 1 2 3 4 5 6 7 8 9 10 @end:0

I would have expected the value 11 in the 2nd log (after the end of the loop), just like in C, for example. Can any of you (also in other Lazarus versions) reproduce this?

When working with FPC 3.2.3 (Lazarus 4.0 RC1) I noticed that the variable handling in for loops does not work as expected:

for i:=0 to 10 do Log(IntToStr(i));
Log('@end:'+IntToStr(i));

returns:

(compiler optimization level 0):   0 1 2 3 4 5 6 7 8 9 10 @end:10
(compiler optimization level 1):   0 1 2 3 4 5 6 7 8 9 10 @end:10
(compiler optimization level 2):   0 1 2 3 4 5 6 7 8 9 10 @end:10
(compiler optimization level 3):   0 1 2 3 4 5 6 7 8 9 10 @end:0

I would have expected the value 11 in the 2nd log (after the end of the loop), just like in C, for example. Can any of you (also in other Lazarus versions) reproduce this?

Share Improve this question edited 18 hours ago AmigoJack 6,1072 gold badges19 silver badges34 bronze badges asked Feb 17 at 22:43 Hannes BrockmannHannes Brockmann 72 bronze badges 1
  • Never expect that for for loops - at worst the compiler optimization reverses the iteration (and having a loop target of 0 can be faster CPU wise). If you rely on the variable's content then use while and repeat loops only. This also applies to other compilers, like Native and Delphi. – AmigoJack Commented 14 hours ago
Add a comment  | 

2 Answers 2

Reset to default 2

From the Free Pascal Wiki:

Note that the value of the "for loop variable" is undefined after a loop has completed or if a loop is not executed at all.

Since you're running the loop to completion, the value is undefined. Optimization level 3 leaves it at the initial value (it's probably unrolling the loop and not updating the variable at all) while others simply omit the last increment.

Yes, according to wiki it is normal. The compiler also points out that i may not be initialized. However, this is incorrect in this case, as i is a local variable that was initialized at the start of the for loop, even if the end value was specified as the start value. I pointed this out because it is handled differently in other programming languages, which has the advantage that the value following the loop end value is directly available. Here you can help yourself by calculating end value+1 yourself and using it in the 2nd log. if the loop is exited prematurely, for example, you do not know up to which value the loop was run through.

发布评论

评论列表(0)

  1. 暂无评论