[fpc-pascal] Using for-loop index variable after loop

Vojtěch Čihák vojtech.cihak at atlas.cz
Mon Oct 10 14:07:35 CEST 2016


Hi, if you mean this:
 
function CategoryIndexOf(Name: String): Integer;
  var
    i: Integer;
  begin
    for i:= lstCategory.Items.Count-1 downto 0 do begin
      if lstCategory.Items[i] = Name
      then Break;
    end;
    Result := i;
    if (i = lstCategory.Items.IndexOf(cActionListEditorUnknownCategory))
       and (i = lstCategory.Items.IndexOf(cActionListEditorAllCategory))
    then Result := 0;
  end;
 
It can IMO cause random and hardly reproducible bugs.
 
V.
______________________________________________________________
> Od: Juha Manninen <juha.manninen62 at gmail.com>
> Komu: FPC-Pascal users discussions <fpc-pascal at lists.freepascal.org>
> Datum: 10.10.2016 12:16
> Předmět: [fpc-pascal] Using for-loop index variable after loop
>
I have always avoided using a for-loop index variable after the loop.
 I thought its value can be unpredictable in some situations when the
 compiler reuses its register or memory etc.
 
 I noticed the ActionListEditor in Lazarus sources has code that uses it after the loop.

 Juha
 _______________________________________________
 fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
 http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal <http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20161010/7702ad1e/attachment.html>


More information about the fpc-pascal mailing list