Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird For loop behaviour 4

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
Hi,

I have the following code in an OnShow event of Form1. Instead of i initialising to 1 and incrementing, i starts at 1000 and decrements. I've never before seen this weird behaviour from a for loop. Can anyone help?

procedure TForm1.FormShow(Sender: TObject);
Const
ARRAY_LENGTH = 1000;
Var
i: Integer;
kd: Array[1..ARRAY_LENGTH] Of Integer;
begin
for i := 1 to ARRAY_LENGTH do
kd := kd[i-1];
end;

 
Turn off Optimisation in Project/Options menu on Compiler page.

--- markus
 
Stretchwickster before you post code put it between brackets
[ /code] etc else your text will become italics Steven van Els
SAvanEls@cq-link.sr
 
The line
Code:
  kd := kd[i-1];
doesn't compile. The left hand side is an array and the right hand side is an integer. Also when the value of i becomes equal to 1 you will get a range check error (because 1 - 1 is equal to 0 and kd[0] is invalid).

The reason why the compiler generates code that starts at 1000 and then decrements towards zero is that the generated code is both smaller and faster.

I'm assuming that you are not familiar with assembler language.

This is roughly what is generated by the compiler when going from 1000 to 1
Code:
  Load 1000 into index register
Loop:
  Do the assignment whatever it should be
  Decrement the index register by 1
  Jump back to Loop if index register is not zero

The alternative is
Code:
  Load 1 into index register
Loop:
  Do the assignment whatever it should be
  Increment the index register by 1
  Test if index register is greater than 1000
  Jump back to Loop if Test is not true

The alternative has one extra instruction per loop iteration. This is because a test for equality to zero is built into the CPU instruction set whilst a test for equality to 1000 is not.

Andrew

 
You are trying to do this:

Code:
for i := 1 to ARRAY_LENGTH do
kd[i] := kd[i-1]


kd[0] doesn't exists, THE COMPILER IS GETTING CONFUSED

maybe it should read
Code:
for i := 2 to ARRAY_LENGTH do
kd[i] := kd[i-1]
Steven van Els
SAvanEls@cq-link.sr
 
Sorry peeps...

That line should've read:
Code:
 kd[i] := i;
Basically, it was just an arbitary example to simply explain what I was getting at.

Thanks Andrew for the Assembly tuition...haven't looked at assembly since I was 16-18!!

It was optimization that was causing it. It just seemed very confusing when I was debugging cos I hadn't noticed it doing that before!!!

Thanks everyone :)
 
Yeah, it's a little more efficient to make a loop run to zero. The problem is that there has been a bug in the debugger ever since this behavior was introduced--examining the loop variable *SHOULD* produce the result of "value unavailable due to optimization" or however that message is worded.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top