Sign in Join

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

Mailing lists / fpc-pascal / 2009-01

How to allocate a 2D array?

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Arjan van Dijk

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

Reply by email

Ivo Steinmann
to Arjan van Dijk

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);
Show quoted text 48 lines
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 - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Jilani Khaldi
to Arjan van Dijk: Hi! In my code I often use 2D arrays. Until today, I kept the maximum dimension fixed to…
Arjan van Dijk wrote 14 lines
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;
You can use a dynamic array.
type
   ColumnType = array of float;
var
   CT: ColumnType;
begin
   SetLength(CT,TheSizeYouWant); // TheSizeYouWant is the number of
elements (an integer)
   CT[0] := 3.14159; // the first element has index 0
...
   CT[TheSizeYouWant-1] := 2.71828; // the last element;
...
SetLength(CT,0); // set it to nil
end;
Jilani KHALDI --------------------- http://jkhaldi.oltrelinux.com

Reply by email

leledumbo
to Jilani Khaldi
He wants 2D dynamic arrays, let's give him one:
type
  TMyArray = array of array of Float;
var
  MyArray: TMyArray;
begin
  SetLength(MyArray,Width,Height); // Valid index now:
[0..Width-1,0..Height-1]
  // Do something with MyArray
  // No deallocation needed, the compiler does this for you
end;
View this message in context: http://www.nabble.com/How-to-allocate-a-2D-array--tp21414552p21429315.html Sent from the Free Pascal - General mailing list .

Reply by email