Displaying Values

We use versions of program InsertionSort to demonstrate several debugging features. The compiler message and runtime error message displayed when compiled and run within the Lazarus IDE are shown in the following screenshot:

Lazarus Messages

Lazarus Messages

The code of this version of the program follows.

program InsertionSort1;
  {$Apptype Console}{$R+}
uses
  SysUtils;
var
  PosNums : array[1 .. 16] of integer =  (503, 087, 512, 061, 908,
                  170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703);

procedure Display(iArray : array of integer);
var
  Count : integer;
begin
  for Count := 0 to high(iArray) do
    write(iArray[Count] : 5);
  writeln;
end;

procedure Sort(var intArray : array of integer);
var
  Inserted : Boolean;
  Current, Insert, Temp : integer;
begin
  if high(intArray) < 1 then
    begin
      writeln('No sorting possible');
    end
  else
    begin
      for Insert := 1 to high(intArray) do
        begin
          Inserted := False;
          Current := Insert;
          Temp := intArray[Insert];
          while not inserted do
            begin
                if Temp > intArray[Current - 1] then
                  begin
                    Inserted := True; //Position found;
                    intArray[Current] := Temp;
                  end
               else
                 begin
                   //Copy integer to the next position in the array
                   intArray[Current] := intArray[Current - 1];
                   dec(Current);
                 end;
            end;//while
        end;//for
     end;//if
end;//proc

begin
  writeln('Initial array of positive numbers:');
  Display(PosNums);
  Sort(PosNums);
  writeln('Sorted array of positive numbers:');
  Display(PosNums);
  readln;
end.

Now we step through this code in Delphi 7 from the start. We press F7 three times, F8 once (to "step over" the display procedure), and then F7 eleven times. We have almost reached the point where the error occurs. We position the cursor over the variable "current" and obtain the following screenshot.

Value of current

Value of current

In order to view the values of several variables at the same time, we can view the Watch debug window and add the variables to the watch. We obtained the following screenshot from Delphi 7.

Watch window

Watch window

To view the Watch window in Delphi or Lazarus, press CTRL+ALT+W or select menu item View > Debug Windows > Watches. To add to the watch list, press CTRL+F5 (or select menu item Run > Add Watch...) then enter the name of the variable in the Expression edit box. Alternatively, you can enter for evaluation an expression such as intArray[Comment-1].

We have paused the program before it has attempted to evaluate intArray[-1]. Interestingly, despite this flaw, the program compiles. We can correct the code by inserting the following code after the instruction dec(Current).
if Current = 0 then
  begin
    intArray[0] := Temp;
    Inserted := True;
  end;

The following section describes how to set breakpoints, thereby saving many F7 and F8 key presses.

Programming - a skill for life!

Stepping through code, setting breakpoints, error handling and range checking