[fpc-pascal] How to allocate a 2D array?

Ivo Steinmann ivo_steinmann at gmx.net
Mon Jan 12 16:27:29 CET 2009


you can try

var
  YourArray, Row: PFloat;
  Width: Integer;
  Height: Integer;

// alloc array
GetMem(YourArray, Width*Height*Sizeof(Float));

// access point (X, Y), while X in [0,Width-1] and Y in [Y, Height-1]
YourArray[X + Y*Width] := ...

// access row (Y)
Row := @YourArray[Y*Width];
Row[X] := ...

// free your array
FreeMem(YourArray);


Arjan van Dijk schrieb:
>
> Hi!
>
> In my code I often use 2D arrays.
> Until today, I kept the maximum dimension fixed to 400 * 300 points.
> How can I make allocatable columns of ARBITRARY size?
>
> For simplicity here a 1D reduction of the problem:
>
> This is what I have:
>
> CONST
>   MaxN = 400;
> TYPE
>   ColumnType = ARRAY[1..MaxN] OF Float;
>
> Recently, some of my grids were so large that having all my grids use 
> the same maximum size
> would cost too much memory. Therefore, I would like to dynamically 
> allocate space
> when the actual dimensions of a grid are known. I found:
>
> TYPE
>   ColumnTypePtr = ^ColumnType;
> VAR
>   Column : ColumnTypePtr;
> ...
> NEW(Column);
>
> and now I have a column of size 400.
>
> --> Question: How can I make allocatable columns of ARBITRARY size?
>
> Thanks,
>
>
> Arjan
>
>
>
> _
> __
> Disclaimer RIVM_ <http://www.rivm.nl/disclaimer.htm>
> ------------------------------------------------------------------------
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal




More information about the fpc-pascal mailing list