PDA

View Full Version : fixing stubborn FOR loops



masonwheeler
23-07-2007, 04:00 PM
I've got a for loop that goes "for i := 0 to arrayProperty.count - 1 do", and it's supposed to only run if there's at least one unit in the array property being counted. (If the array's size is 0, then the expression evaluates to -1, which is less than 0, and so the for loop should never enter the first iteration.) I know this is supposed to work, because I've done it in the past. But for some reason, now all of a sudden it's entering the first loop no matter what, and sometimes trying to access item [0] of an array that doesn't contain anything.
I figure one of the settings must have come loose somewhere. :P How do I fix it so it'll behave correctly and not enter the loop the first time unless the condition evaluates true?

Mason

Robert Kosek
23-07-2007, 04:48 PM
if arrayProperty.count > 0 then
for i := 0 to arrayProperty.count - 1 do

masonwheeler
23-07-2007, 04:50 PM
Yeah, that's the obvious workaround, but how do I get it to evaluate at the top of the loop in the first place?

Mason

WILL
23-07-2007, 05:04 PM
You don't. At least you shouldn't anyway.

One of the truths of dealing with 0-based arrays.

tpascal
23-07-2007, 05:31 PM
"for i := 0 to arrayProperty.count - 1 do",


you should debug printing on screen what value is "i" geting the first time interaction,

I think "i' have to be signed so it can get -1 value; if it is defined as unsigned type (word, byte, longword, etc) then you will get a problem.

"arrayProperty.count - 1" should return -1 when arrayProperty.count = 0 if this is not returning a signed type then you are having a problem;

masonwheeler
23-07-2007, 05:59 PM
Oh! That's the problem, all right. My i was a word. So it was probably evaluating -1 as MAXINT16 -1, (or whatever it's called,) which is a big positive number.

Thanks!

Mason

LP
23-07-2007, 06:55 PM
I've got a for loop that goes "for i := 0 to arrayProperty.count - 1 do", and it's supposed to only run if there's at least one unit in the array property being counted. (If the array's size is 0, then the expression evaluates to -1, which is less than 0, and so the for loop should never enter the first iteration.) I know this is supposed to work, because I've done it in the past. But for some reason, now all of a sudden it's entering the first loop no matter what, and sometimes trying to access item [0] of an array that doesn't contain anything.
I figure one of the settings must have come loose somewhere. :P How do I fix it so it'll behave correctly and not enter the loop the first time unless the condition evaluates true?
IMHO this is an internal error of Delphi 7 compiler (it hasn't occured to me after I moved away from Delphi 7). Usually changing the code somewhere else, moving some parts, etc., exiting Delphi and running it again, then rebuilding the application fixes the problem.

Also make sure you don't overwrite memory in your application as this may trigger weird things to happen like "initialization" code in your unit not being executed, etc.

masonwheeler
23-07-2007, 06:57 PM
I'm using BDS 2006. And Tpascal had the solution. But thanks for trying to help. :)

Mason

JernejL
23-07-2007, 07:53 PM
how about for i:= low() to high() do begin..??

masonwheeler
23-07-2007, 07:55 PM
2 reasons. 1, efficiency. No need to waste cycles on low() when I know it's a zero-based array. 2, I'm not sure what would happen if I tried that on an empty array property.

Mason

marcov
29-07-2007, 02:54 PM
2 reasons. 1, efficiency. No need to waste cycles on low() when I know it's a zero-based array. 2, I'm not sure what would happen if I tried that on an empty array property.

Mason

low and high are built in functions, and generally no code is generated for it. (the exception is high with open arrays, and even there it is only one instruction)

But you can only use it for arrays, not for array properties.