Using for-loop index variable after loop

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Using for-loop index variable after loop

Juha Manninen
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.
The compiler, FPC 3.0, does not warn about it.
I made a small test:

procedure MyProcedure;
var
  i: Integer;
begin
  for i := 0 to 5 do
    WriteLn('iii ', i);
  Write('After loop i=', i);
  if i = 5 then
    WriteLn(' as expected')
  else
    WriteLn(' which is unpredictable');
end;

Indeed, no warning!
Is it OK to do that? Have I misunderstood it?

Juha
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Using for-loop index variable after loop

Jonas Maebe-2

Juha Manninen wrote on Mon, 10 Oct 2016:

> 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.

That is correct.

> The compiler, FPC 3.0, does not warn about it.

Indeed: http://bugs.freepascal.org/view.php?id=12508


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Using for-loop index variable after loop

vojtech.cihak
In reply to this post by Juha Manninen

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 <[hidden email]>
> Komu: FPC-Pascal users discussions <[hidden email]>
> 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  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Using for-loop index variable after loop

Juha Manninen
On Mon, Oct 10, 2016 at 3:07 PM, Vojtěch Čihák <[hidden email]> wrote:
> Hi, if you mean this:
> ...

Yes, that is one of them.
The code does not make sense in any case, regardless of "i"'s value.
Index for cActionListEditorUnknownCategory and
cActionListEditorAllCategory differ.
"i" cannot have both at the same time. Maybe the condition should be
"or", not "and".
Why does it not compare Name directly with
cActionListEditorUnknownCategory and cActionListEditorAllCategory
constants?

There are many similar fishy code blocks. I will sanitize some of them ...

Juha
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Using for-loop index variable after loop

Virgo Pärna
In reply to this post by vojtech.cihak
On Mon, 10 Oct 2016 14:07:35 +0200, Vojtěch Čihák <[hidden email]> wrote:
>       then Break;
>  

        Isn't leaving for loop with break only case, where loop variable
will keep the value. Otherwise it is undefined.
        But it does make the code more confusing.

--
Virgo Pärna
[hidden email]

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal