Board index » delphi » Creating Arrays of Variable Size at Runtime

Creating Arrays of Variable Size at Runtime

Hi,

Although I'm using Delphi this is probably an Object Pascal question.

Is it possible to create an array of variable size at runtime?

In Oberon eg one would write:

TYPE
  ArrayPtr = POINTER TO REAL;
  MatrixPtr = POINTER TO ArrayPtr;
VAR
  vsizeArray: ArrayPtr;
  matrixArray: MatrixPtr;
...
{Now create array at runtime}
NEW(vsizeArray, 100);
{WE now have an array of 100 items rangin from 0..99}
NEW(matrixArray, 5, 100)
{We now have a two dimensional array}

Is something like this possible in Object Pascal, or do I still have to
use:

TYPE
  LargeArrayPtr = ^LargeArray;
  LargeArray = ARRAY[1..1500] OF REAL;

VAR
  someArray = LargeArrayPtr;

and then create this array dynamically??? Which means I will always
have to anticipate the largest possible size.

 

Re:Creating Arrays of Variable Size at Runtime


During Friday August 18 1995, paul sjoerdsma transmogriphed to All about
Creating Arrays of Variable Size at Runtime:

 ps> Although I'm using Delphi this is probably an Object Pascal question.
 ps> Is it possible to create an array of variable size at runtime?

IF you do like you currently do, allocating an array of fixed (known) max
items, then defining on which is closest to 64K will do the trick if you don't
use NEW(); but GETMEM();

 ps> Is something like this possible in Object Pascal, or do I still have to
 ps> use:

 ps> TYPE
 ps>   LargeArrayPtr = ^LargeArray;
 ps>   LargeArray = ARRAY[1..1500] OF REAL;

 ps> VAR
 ps>   someArray = LargeArrayPtr;

GETMEM(SomeArray,4*ItemsYouNeed);
{4= size of a pointer, you can substitute "4" with "SIZEOF(POINTER)" as it is
more 'accurate' I think.}

Now to use it DON'T adress items higher than allocated.
deallocating requires FREEMEM is I think it also requires the size of the item
freed so look these commands up in the online help to check out the exact way
to call them.

 ps> and then create this array dynamically??? Which means I will always
 ps> have to anticipate the largest possible size.

You still have to anticipate the max possible (or allowed) items. But if you
don't allow Pascal's runtime check for your adressing you can make an array of
1 item and do the checking yourself - the point being not to allocate an array
larger than 64K and using it not to adress items extending the amount
allocated.

I hope I do not have to tell you this is completely untested and as it so
happens out of my head. But i think it is worth a try - I vagely remember
toying with this approach 4 years ago.

Maybe you'll find som freedom in this approach if it works, maybe you'll find
it tedious to do the checking yourself.

Regards
-Mikkel Breiler (Vinyl/iNTUiTiON & Sofi/DiskDoublers)
InterNet: mbrei...@renoz.ping.dk. Fido: 2:236/501.28, 2:236/86.28.

Re:Creating Arrays of Variable Size at Runtime


What I usually do is:

type

 array_of_whatever : array [0..0] of whatever;

 array_of_whatever_pointer : ^array_of_whatever;

var

 an_array_of_whatever : array_of_whatever_pointer;

 getmem (an_array_of_whatever,n*sizeof(whatever));

 an_array_of_whatever^[index] := a_whatever;

 freemem(an_array_of_whatever);

The only trick with this scheme is you have to
turn off range checking, at least during
the code that references the array

--
Chris Kushmerick
<ckushmer...@ccmail.sunysb.edu>

Other Threads