[fpc-pascal]Re: "for" loop questions

XHajT03 at mbox.vol.cz XHajT03 at mbox.vol.cz
Sun Dec 8 11:00:27 CET 2002


From:           	"Balogh, Karoly (Charlie/iNQ)" <charlie at scenergy.dfmk.hu>
To:             	fpc-pascal at deadlock.et.tudelft.nl
Date sent:      	Sun, 08 Dec 2002 10:44:45 +0100
Subject:        	[fpc-pascal]Re: "for" loop questions
Send reply to:  	fpc-pascal at deadlock.et.tudelft.nl

> >> But you should. :) I don't know the type of L.Count, but i suppose
> >> it's unsigned. If L.Count is 0, and you try to count until
> >> L.Count-1, it it simply underflows at the -1 operation. And i will
> >> count to $FF, $FFFF or $FFFFFFF, or whatever, depending on the type
> >> of L.Count. 
> > This isn't really correct either; this would only be true if you
> > assign the result of subtraction to an unsigned variable; the
> > calculation itself is done after conversion to a signed type.
> 
> Is this true in every compiler mode? :) I believe to you, but then i
> have no idea why i got some crashes with some cases very similar to
> this in my own sources... Since the loop doesn't get executed then. I
> simply added the checks, to avoid this case, because i thought it's
> trivial why it crashes... 

 It depends how much similar those cases were... ;-) The following 
code _will_ crash, and it's still quite "similar" to the previous 
one:

{$MODE DELPHI}
uses
 Classes;
var
  L : TList;
  P : Pointer;
  I : Integer;
  W: word;
begin
  L := TList.Create;
  W := L.Count - 1;
  for I := 0 to W do begin
    P := L[I];
  end;
end.


Tomas





More information about the fpc-pascal mailing list