From: allan@u9.oslohd.no (Allan Lochert)
Subject: Re: [delphi] ?
Date: Thu, 20 Jul 95 19:44:13 +0100

>>Is it possible to declare the range of an array at run-time?

Well, you can create dynamic arrays in Delphi, and they can be declared at runtime.

type
   MyRecord = Record
       aString : String;
       aByte   : byte;
   end;

   PMyRecord = ^MyRecord;

   MyRecordArray = Array[0..0] of PMyRecord;

var
   MyArray : MyRecordArray;

begin
   GetMem(MyArray,5000*sizeof(MyRecord));  {Create array with 5000 elements }
   
   MyArray^[1].aString:='This is a string';
   

This is at least close to how to do it. The above is just quick whithout
looking it up, so there might be some typos.

One advantage of this is that the total size of all array elements can be greater
than 64KB, in fact you can have 32k elements max, but only you heap size is limiting
the total allocated memory.

Or, another way of doing dynamic arrays in delphi is by using TList, which is very
easy to use and has the functionality of an array.

Regards
Allan

-------------------------------------------------------------------------------

From: Ben Licht <benl@panix.com>
Subject: Re: [delphi] ?
Date: Fri, 21 Jul 1995 06:51:32 -0400

So far as I know, neither of the solutions as they are written will work 
correctly. The first one wont work because you are using GetMem to allocate
the space, which has a max allocation of about 64K.  The second method will
not work for two reason, I have found that Delphi says that the stucture is
too large.  Secondly, you are still taking memory away from your data segment
with this method (each element in the array takes away 4 bytes).  I with a
minor adjustment to both these methods, however, you can achieve the desired
goal:

type
  THa = array[0..0] of string;
  PHa = ^THa;

procedure TForm1.Button1Click(Sender: TObject);
var
  h : THandle;
  MyArray : PHa;
  x : longint;
begin
  h := GlobalAlloc(GMEM_FIXED, 10000*sizeof(THa)); {allocate 10000 elements}
  MyArray := GlobalLock(h);
  x := 1000;
  MyArray^[x] := 'element #1000';
  x := 9567;
  MyArray^[x] := 'Element #9567';
  x := 1000;
  writeln(MyArray^[x]);
  x := 9567;
  writeln(MyArray^[x]);
  {You should free the memory here}
end;

Note:  By using an indexed property, you could avoid having to use a variable
to reference the array index, and you would not need to dereference the array
pointer.

								HTH,

									Ben
