Board index » delphi » Help please - Dynamic arrays

Help please - Dynamic arrays

Could someone please tell me how to declare a dynamic array
in delphi?  i.e. declare an array using run-time variables
for the array size.

   Many thanks

 

Re:Help please - Dynamic arrays


Quote
howie...@vnet.ibm.com (howie...@vnet.ibm.com) wrote:

| Could someone please tell me how to declare a dynamic array
| in delphi?  i.e. declare an array using run-time variables
| for the array size.

This is really a pascal question, but:

type
        TDynamicArray = Array[0..10000] of ElementType;
var
        AnArrPtr: ^TDynamicArray;
begin
  GetMem(AnArrPtr, SizeOf(ElementType) * NumOfElements);
  ...
  {refer to array as AnArrPtr^[<index>]}
  ...
  FreeMem(AnArrPtr, SizeOf(ElementType) * NumOfElements);
end;

Note, there is no range checking. Choose array size in the TDynamicArray
type to be larger than the largest array you'll use.

--
Joseph S. Bui's Standard Disclaimer:
I love you.

Re:Help please - Dynamic arrays


As far as I know you can't declare dynamic arrays in Delphi. It is not a
feature supported by the Pascal language. If you have come from VB and
are looking for a way to store data similarly to dynamic arrays in VB
then I suggest you use a linked list of records. Dynamic, and much more
useful than dynamic arrays. Here's an example declaration for a doubly
linked list:

TYPE

PMyType = ^MyType;         { pointer to MYTYPE }

MyType = record
    MyInt: Integer;        { integer field of record }
    MyString: String;      { string field of record }
    NextMyType: PMyType;   { points to next record }
    PrevMyType: PMyType;   { points to previous record }
end;

Check out the 'New' and 'Dispose' functions in the Online help.

Phil Middlemiss
phill...@tawa.fri.cri.nz
NZ Forest Research Institute
New Zealand

Re:Help please - Dynamic arrays


In message <DCxrFn.3...@austin.ibm.com>
        howie...@vnet.ibm.com writes:

Quote
> Could someone please tell me how to declare a dynamic array
> in delphi?  i.e. declare an array using run-time variables
> for the array size.

Have a look at 'TList' in the on-line help.  Looks to give you all
the properties of a dynamic array and more.

---
Stefan
stefan.ka...@zetnet.co.uk

Re:Help please - Dynamic arrays


Quote
Phillip Middlemiss <phill...@tawa.fri.cri.nz> wrote:
>As far as I know you can't declare dynamic arrays in Delphi. It is not a
>feature supported by the Pascal language. If you have come from VB and
>are looking for a way to store data similarly to dynamic arrays in VB
>then I suggest you use a linked list of records. Dynamic, and much more
>useful than dynamic arrays. Here's an example declaration for a doubly
>linked list:

>TYPE

>PMyType = ^MyType;         { pointer to MYTYPE }

>MyType = record
>    MyInt: Integer;        { integer field of record }
>    MyString: String;      { string field of record }
>    NextMyType: PMyType;   { points to next record }
>    PrevMyType: PMyType;   { points to previous record }
>end;

>Check out the 'New' and 'Dispose' functions in the Online help.

>Phil Middlemiss
>phill...@tawa.fri.cri.nz
>NZ Forest Research Institute
>New Zealand

Alternatively, you could use TList, or derive a class from it,
which makes things look a lot like dynamic arrays.  Plus, you don't
have to do loads of pointer-chasing to find stuff and do all the messing
about that is needed when you insert od delete things.

-----------------------------------
Matt Francomb, Setanta Software Ltd
http://www.demon.co.uk/setanta
-----------------------------------

Other Threads