# Board index » delphi » Two Dimensional Dynamic array

## Two Dimensional Dynamic array

Hi all,

I'm trying to implement a dynamic two dimensional array and
I was wondering if anyone knows why the following bit of code
continually produces a GPF :

type
DoubleArray = array[0..0] of double;

var
TwoDArray : ^DoubleArray;

begin
GetMem(TwoDArray, (Rows + 1) * (Cols + 1));
{ Initialise all elements to 0 }
For i := 0 to Rows do
For j := 0 to Cols do
Results^[i,j] := 0;
...
end;

Thanks for any help.

Phil.

--
/----------------------------------------------------------------\
| Phillip Middlemiss            | 2 wrongs don't make a right... |
| NZ Forest Research Institute  |                                |
| phill...@tawa.fri.cri.nz      |   .....but three lefts do      |
\----------------------------------------------------------------/

## Re:Two Dimensional Dynamic array

phill...@fri.cri.nz (Phillip Middlemiss)  wrote:

##### Quote
> Hi all,

> I'm trying to implement a dynamic two dimensional array and
> I was wondering if anyone knows why the following bit of code
> continually produces a GPF :

> type
>      DoubleArray = array[0..0] of double;

hey! ^^^^
of course to be twodimensional this should be:
array[0..0,0..0] of double;
##### Quote

> var
>      TwoDArray : ^DoubleArray;

> begin
>   GetMem(TwoDArray, (Rows + 1) * (Cols + 1));

ok. But where is Rows and Cols defined ?

##### Quote
>   { Initialise all elements to 0 }
>   For i := 0 to Rows do
>     For j := 0 to Cols do
>       Results^[i,j] := 0;

here also ^^^^^^
it should be:
TwoDArray^[i,j] := 0.0;
##### Quote
> ....
> end;

> Thanks for any help.

> Phil.

> --
> /----------------------------------------------------------------\
> | Phillip Middlemiss            | 2 wrongs don't make a right... |
> | NZ Forest Research Institute  |                                |
> | phill...@tawa.fri.cri.nz      |   .....but three lefts do      |
> \----------------------------------------------------------------/

hth
Wolfgang Zehntner

## Re:Two Dimensional Dynamic array

##### Quote
zehnt...@woze.in-passau.de (Wolfgang Zehntner) wrote:
>phill...@fri.cri.nz (Phillip Middlemiss)  wrote:
[snip]
>> I'm trying to implement a dynamic two dimensional array

>> type
>>      DoubleArray = array[0..0] of double;
>                      hey! ^^^^
>of course to be twodimensional this should be:
>                     array[0..0,0..0] of double;

Uhhh... this will not work either, since the compiler does not know
how big each row is. I would recommend creating a dynamic array
component, sortof like this:

{------------------}

type
TMatrix = class
private:
FArray: Pointer;
FRows, FCols: Integer;
function GetItem(R, C: Integer): extended;
procedure SetItem(R, C: Integer; Value: extended);
public
property Array[ R, C: Integer ] as extended
constructor Create(R, C: Integer); override;
destructor Destroy; override;
end;

constructor TMatrix.Create(R, C: Integer);
begin
inherited Create;
GetMem(FArray, SizeOf(Extended) * R * C);
FRows := R;
FCols := C;
end;

destructor TMatrix.Destroy;
begin
Dispose(FArray);
inherited Destroy;
end;

function GetItem(R, C: Integer): extended;
type
PEx = ^Extended;
begin
{check for out of bounds}
Result := Pex(Long(Array) + (C + R * FCols) * SizeOf(Extended))^;
end;

procedure SetItem(R, C: Integer; Value: extended);
type
PEx = ^Extended;
begin
{check for out of bounds}
Pex(Long(Array) + (C + R * FCols) * SizeOf(Extended))^ := Value;
end;

{----------------------}

Use it like this:

var
A: TMatrix;
begin
A := TMatrix.Create(20, 20);

A[0,0] := 1;
WriteLn('A[0,0] =', A[0,0]);

A.Free;    {don't forget this! otherwise you leak 4k right here. }
end;

This approach allows you to encapsulate the actual low-lying code...
you could store the values in an array of linked lists if it were a
sparse array, without having to change your code.

-Tony Lownds <lown0...@maroon.tc.umn.edu>