Forum > FPC development

KYLIX LOOP VS FPC LOOP [SEVERE BUG?!]

(1/2) > >>

Snify:
I am currently working on a project where I port Kylix Code to FPC.
Now, I have discovered a very strange thing...
( I have tested this only with Kylix - I am not sure about other Delphi Versions or loops...)
My FPC Project is configured as Linux-32bit with the regular Ansi Delphi Syntax for Kylix compatibility.

However a regular "for" loop counter behaves differently in Kylix than in Lazarus.

For exmaple (a simple loop):


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---     for i := 1 to 6 dobegin // some non critical operations     end;ShowMessage (inttostr(i));  
Kylix shows a value of 7 whereas FPC shows a value of 6 - at the end of the loop!
However, if you "BREAK" the loop in between (e.g. if i = 6 then break) - the value stays at 6 on both compilers.

So Kylix (for whatever reason) adds an extra value AFTER the loop has been SUCCESSFULLY finished.

This is a severe behavior throughout all loops in the project..

Could someone please give me any advice on how to solve/emulate this behavior in Lazarus, without much effort?
Is it possible to let the FPC developer know about this, so they may fix/emulate this?

Thank you so much in advance.

howardpc:
Outside a for loop the loop variable is undefined, and any value it has is implementation-dependent (as you found).
You can always make an explicit assignment:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1; {$mode delphi} uses  SysUtils; var  i, KylixEmulation: Integer; begin  KylixEmulation := 1;  for i := KylixEmulation to 6 do    begin      Inc(KylixEmulation);    end;  WriteLn('i: ',i,' KylixEmulation: ',KylixEmulation);  ReadLn;end.

Seenkao:
Yandex translate:
use the "while" loop, there will be the necessary behavior.

Snify:

--- Quote from: howardpc on October 18, 2021, 03:12:59 pm ---Outside a for loop the loop variable is undefined, and any value it has is implementation-dependent (as you found).
You can always make an explicit assignment:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1; {$mode delphi} uses  SysUtils; var  i, KylixEmulation: Integer; begin  KylixEmulation := 1;  for i := KylixEmulation to 6 do    begin      Inc(KylixEmulation);    end;  WriteLn('i: ',i,' KylixEmulation: ',KylixEmulation);  ReadLn;end.
--- End quote ---

Thank you for your answer... I understand it.
So I guess, it is up to me to look through all the loops in the project and fix this.
Never use it outside the loop  :'(

Is there maybe an automated approach or so to "find" all loops in a source file? (without searching for keywords like while/for/repeat/etc....)

marcov:

--- Quote from: Snify on October 18, 2021, 03:00:10 pm ---However a regular "for" loop counter behaves differently in Kylix than in Lazarus.

--- End quote ---

Doesn't matter. How is it /documented/ in Kylix?

Adhoc testing might depend on optimization level, architecture and codegeneration pecularities. IOW it might be a coincidence that your Kylix code always seems to do this, but not hold true always over all options, versions  etc. 

To my best knowledge, all Pascal's specify the loopvar after the for loop as undefined, with the exception of a BREAK/exit/goto in the for loop (in which case the for loop is more while loop like). This also is true for all Delphi versions, so I have no reason it was different for Kylix which was mostly D6/7 like.

So the bug is actually in relying on this coincidental behaviour.

E.g. the Delphi help says:


--- Quote from: https://docwiki.embarcadero.com/RADStudio/Sydney/en/Declarations_and_Statements_(Delphi)#For_Statements ---After the for statement terminates (provided this was not forced by a Break or an Exit procedure), the value of counter is undefined.

--- End quote ---

Navigation

[0] Message Index

[#] Next page

Go to full version