Solved

for loop is counting backwards

Posted on 2000-04-04
12
356 Views
Last Modified: 2010-04-04
Consider the following code:

procedure TFormMain.ButtonReJARClick(Sender: TObject);
var
  counter1 : integer;
  FileFailed : boolean;
  temp1 : string;
  temp2 : string;
begin
  temp1 := PathToFileToAdd;
  temp2 := JARFileCandidates[0].GetFilePath;
  FileFailed := false;
  for counter1 := 2 to 40 do
  begin
    if JARFileCandidates[counter1].GetReplaceStatus = true then
      CopyFile(pchar(PathToFileToAdd), pchar(JARFileCandidates[counter1].GetFilePath), FileFailed);
  end;
end;

As I step through this code in the debugger, counter1 is STARTING at 40 and counting BACKWARDS but I am not using "down to" in my for loop.

Can anyone explain this strange behavior to me?

0
Comment
Question by:knowlton
  • 6
  • 5
12 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 50 total points
ID: 2685725
This is caused by the code optimizer. Turn it off in the Project Menu > Options > Compiler tab > Optimization.

Make sure that your code is re-compilered after you changed this option.


Cheers,

Epsylon.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2685732
Of course I mean 're-compiled'   :o)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2686335
:-) sometimes the optimizer is a bit silly
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 5

Author Comment

by:knowlton
ID: 2687067
Epsylon:

Why do the other for loops function properly then?  What is it about this particular for loop that makes the optimizer "wig-out"????

Thanks.
0
 
LVL 5

Author Comment

by:knowlton
ID: 2687135
From Delphi Help:

The $O directive controls code optimization. In the {$O+} state, the compiler performs a number of code optimizations, such as placing variables in CPU registers, eliminating common subexpressions, and generating induction variables. In the
{$O-} state, all such optimizations are disabled.
Other than for certain debugging situations, you should never have a need to turn optimizations off. All optimizations performed by Delphi's Object Pascal compiler are guaranteed not to alter the meaning of a program. In other words, Delphi performs no "unsafe" optimizations that require special awareness by the programmer.

So, does this mean that these weird issues only arise during debugging, and the compiled .exe won't display the symptoms?

Tom
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2688242
The reason why this happens is that the number of loops is known at compile-time. The assembler code is faster when it counts back to zero. It saves one or 2 instructions this way. In very small loops it could improve the performance up to 50%.
0
 
LVL 5

Author Comment

by:knowlton
ID: 2690099
Hmmmm....okay.  :)
0
 
LVL 5

Author Comment

by:knowlton
ID: 2690104
What is considered to be a "small" for loop?
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2690314
When there is not much code inside the loop. Something like this:

for i := 0 to 100000 do
  buffer[i] := 0;
0
 
LVL 5

Author Comment

by:knowlton
ID: 2690351
Ahhh...my for loop would fit under "small" then.  :)

Now, is it true that this "bug" only displays itself at debug time?  Would the final .exe have this behavior?

Tom
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2690429
Not really. Inside your loop CopyFile is called. The time that it costs even depends in some hardware.

yes, the final exe will have it too. But what does it matter. The optimizer is just very smart and tries to gain performance where it can.
0
 
LVL 5

Author Comment

by:knowlton
ID: 2690479
I suppose this isn't really a big deal, except when I was testing, I knew that my data structure [JARFileCandidates] only had one item in it.  Due to optimization I had to count down through 999 items to get to the one that I wanted to test.

I know that I can set conditionals at debug time that will execute a for loop until the counter equals a certain number, so I guess I could have done that.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question