Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-04

AV exception error on "lower1 := left" (integer variable assignment)... Can anyone explain this???

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Peter E Williams

Hi All,

I am having a problem with a strange run-time AV error. See the link to the Lazarus project.

Download link:
http://hotfile.com/dl/115480488/6b7a8ed/dtm_maker_001.zip.html

The problem is in the QSort procedure:

Procedure QSort( Var island_single_array : island_single_type;
  left, right : integer);
  Var
    d       : island_single_type;
    pivot,
    lower1,
    upper1 :integer;
  begin
    lower1 := left;
// ^^^^^^ AV run-time exception on above line.
    upper1 := right;
    pivot := island_single_array[left].location_number;
    While (left < right) do
    Begin
      Case SortType of
        ascending : begin
                      While (island_single_array[right].location_number >=
pivot) and (left < right) do
                        dec(right);
                      if (left  right) then
                      begin
 swap(island_single_array[left],island_single_array[right]);
                        inc(left);
                      end;
                     While (island_single_array[left].location_number <=
pivot) and (left < right) do
                       inc(left);
                      if (left  right) then
                      begin
 swap(island_single_array[left],island_single_array[right]);
                        dec(right);
                      end;
                   end;
       end; // case
    end; // while
    pivot := island_single_array[left].location_number;
    left := lower1;
    right := upper1;
    if left < pivot then
      QSort(island_single_array,left,pivot-1);
    if right > pivot then
      QSort(island_single_array,pivot+1,right);
  end;  { QSort }
{--------------------------------------------------------------------}
procedure QuickSort( Var island_single_array : island_single_type;
  Size : integer);
begin
  QSort( island_single_array, 1, Size );
end;
{--------------------------------------------------------------------}

The AV exception error occurs after the project is run then the user selects File / Save and the QuickSort procedure is called which then calls Qsort. The error is occurring on:

    lower1 := left;

which are both integers. Lower1 (which was renamed just in case of a name conflict with the 'lower' string function) is ??? (unassigned) and left is
1. I see no problem with this statement and it is a mystery to me why I am getting an AV exception error.

Maybe someone here with better debugging skills can tell me why is this happening and how to fix it.

Best Regards,
Peter / pew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110424/c7f7ccc0/attachment.html>

Peter E Williams
to Peter E Williams

Hi All,

I am having a problem with a strange run-time AV error. See the link to the Lazarus project.

Download link:
http://hotfile.com/dl/115480488/6b7a8ed/dtm_maker_001.zip.html

The problem is in the QSort procedure:

Procedure QSort( Var island_single_array : island_single_type;
  left, right : integer);
  Var
    d       : island_single_type;
    pivot,
    lower1,
    upper1 :integer;
  begin
    lower1 := left;
// ^^^^^^ AV run-time exception on above line.
    upper1 := right;
    pivot := island_single_array[left].location_number;
    While (left < right) do
    Begin
      Case SortType of
        ascending : begin
                      While (island_single_array[right].location_number >=
pivot) and (left < right) do
                        dec(right);
                      if (left  right) then
                      begin
 swap(island_single_array[left],island_single_array[right]);
                        inc(left);
                      end;
                     While (island_single_array[left].location_number <=
pivot) and (left < right) do
                       inc(left);
                      if (left  right) then
                      begin
 swap(island_single_array[left],island_single_array[right]);
                        dec(right);
                      end;
                   end;
       end; // case
    end; // while
    pivot := island_single_array[left].location_number;
    left := lower1;
    right := upper1;
    if left < pivot then
      QSort(island_single_array,left,pivot-1);
    if right > pivot then
      QSort(island_single_array,pivot+1,right);
  end;  { QSort }
{--------------------------------------------------------------------}
procedure QuickSort( Var island_single_array : island_single_type;
  Size : integer);
begin
  QSort( island_single_array, 1, Size );
end;
{--------------------------------------------------------------------}

The AV exception error occurs after the project is run then the user selects File / Save and the QuickSort procedure is called which then calls Qsort. The error is occurring on:

    lower1 := left;

which are both integers. Lower1 (which was renamed just in case of a name conflict with the 'lower' string function) is ??? (unassigned) and left is
1. I see no problem with this statement and it is a mystery to me why I am getting an AV exception error.

Maybe someone here with better debugging skills can tell me why is this happening and how to fix it.

Best Regards,
Peter / pew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110424/8cc5d985/attachment.html>

Mattias Gaertner
to Peter E Williams: I am having a problem with a strange run-time AV error. See the link to the Lazarus…
Show quoted text 12 lines
Hi All, I am having a problem with a strange run-time AV error. See the link to the Lazarus project. Download link: http://hotfile.com/dl/115480488/6b7a8ed/dtm_maker_001.zip.html The problem is in the QSort procedure: [...] pivot := island_single_array[left].location_number;

"pivot" is the key of the pivot element at position "left".

Show quoted text 5 lines
[...] pivot := island_single_array[left].location_number; left := lower1; right := upper1; if left < pivot then

^^^^^^^^^^^^
Comparing a position and a key makes no sense.

You have to keep track of the position of the pivot element. See the quicksort implementation in TFPList.Sort.

Mattias

Peter E Williams
to Mattias Gaertner

Hi Mattias,

Mattias Gaertner wrote 31 lines
On Sun, 24 Apr 2011 08:58:07 +1000 Peter E Williams wrote: > Hi All, > > I am having a problem with a strange run-time AV error. See the link to the > Lazarus project. > > Download link: > http://hotfile.com/dl/115480488/6b7a8ed/dtm_maker_001.zip.html > > The problem is in the QSort procedure: > >[...] > pivot := island_single_array[left].location_number; "pivot" is the key of the pivot element at position "left". >[...] > pivot := island_single_array[left].location_number; > left := lower1; > right := upper1; > if left < pivot then ^^^^^^^^^^^^ Comparing a position and a key makes no sense. You have to keep track of the position of the pivot element. See the quicksort implementation in TFPList.Sort.

Could you please give me a link to the TFPList.Sort code so that I can use that code (via cut and paste) in my unit ?

Thanks and best regards,
Peter / pew

Show quoted text 8 lines
Mattias -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110424/f0df8793/attachment.html>

Mattias Gaertner
to Peter E Williams
[...] Could you please give me a link to the TFPList.Sort code so that I can use that code (via cut and paste) in my unit ?

Simply use Lazarus' find declaration.

Mattias

Graeme Geldenhuys
to Peter E Williams: Could you please give me a link to the TFPList.Sort code so that I can use that code (via…

2011/4/24 Peter E Williams <pewslinuxvault at gm*****>:

Could you please give me a link to the TFPList.Sort code so that I can use that code (via cut and paste) in my unit ?

You must really learn to use the Lazarus IDE. "Find in files" or "View

Code Browser" or grep or "Ctrl + Click" will all get you to the code

you want to see.

Regards, ? - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net