Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to swap two rows of a two-dimensional array, and why would it work?
    primarykey
    data
    text
    <p><strong>Summarization:</strong></p> <p>Please check the comments below from David, Uwe, and other experts.</p> <p>================================================================================</p> <p>The following code swaps two rows in a two-dimensional, dynamic array of double values. I am wondering: (1) whether the following code is a best practice of swapping two rows of a two-dimensional array? If not, then what is the best practice to do this kind of job? (2) why would the following code work? I mean, isn't two-dimensional array a <strike>continuous</strike> contiguous section of memory? Does the following code work only by luck? Any suggestion is appreciated! </p> <pre><code> unit Unit5; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TAADouble = array of array of Double; TForm5 = class(TForm) procedure FormShow(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form5: TForm5; procedure SwapRows(arr: TAADouble; row0, row1: Integer); implementation {$R *.dfm} procedure SwapRows(arr: TAADouble; row0, row1: Integer); var Tmp: Integer; begin {$IFDEF FPC} Tmp := PtrUInt(arr[row0]); PtrUInt(arr[row0]) := PtrUInt(arr[row1]); PtrUInt(arr[row1]) := Tmp; {$ELSE} Tmp := Integer(arr[row0]); Integer(arr[row0]) := Integer(arr[row1]); Integer(arr[row1]) := Tmp; {$ENDIF} end; procedure TForm5.FormShow(Sender: TObject); var tmpArray: TAADouble; I, J: Integer; rowStr: string; begin SetLength(tmpArray, 10, 10); rowStr := ''; for I := 0 to 9 do for J := 0 to 9 do tmpArray[I][J] := I * J; for I := 0 to 9 do begin rowStr := ''; for J := 0 to 9 do rowStr := rowStr + FloatToStr(tmpArray[I][J]) + ' '; OutputDebugString(PWideChar(rowStr)); end; SwapRows(tmpArray, 3, 4); for I := 0 to 9 do begin rowStr := ''; for J := 0 to 9 do rowStr := rowStr + FloatToStr(tmpArray[I][J]) + ' '; OutputDebugString(PWideChar(rowStr)); end; end; end. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload