Board index » delphi » Tips on dynamic arrays, and help with dynamic arrays of records

Tips on dynamic arrays, and help with dynamic arrays of records

Hi,

I've been learning Delphi 3 (Pro) now for around 1 month, and I'd like to
ask some advice.

Basically, I'm not totally sure if I'm creating dynamic arrays correctly.
ATM, I define a variable as a variant, then use VarArrayCreate() to convert
it to an array of the correct size.  Is this the preferred way of using
dynamic arrays or is there a more efficient way?

Also, I'm not sure how to create a dynamic array composed of records.  I
have absolutely no idea how to do this, and there doesn't appear to be any
information in the manuals about it.

(Please can anyone replying to this message also make a CC to my email
address?  I don't read this newsgroup regularly)

Any help will be much appeciated,

Dave.

 

Re:Tips on dynamic arrays, and help with dynamic arrays of records


Quote
>Basically, I'm not totally sure if I'm creating dynamic arrays correctly.
>ATM, I define a variable as a variant, then use VarArrayCreate() to convert
>it to an array of the correct size.  Is this the preferred way of using
>dynamic arrays or is there a more efficient way?

>Also, I'm not sure how to create a dynamic array composed of records.  I
>have absolutely no idea how to do this, and there doesn't appear to be any
>information in the manuals about it.

If VarArrayCreate( ) is working for you, that's certainly one way to handle
this.

But if you're really looking to move into records, you might take a look at
linked lists and TLists.

Re:Tips on dynamic arrays, and help with dynamic arrays of records


In the TCollection class that was in Turbo Pascal in the pre-Delphi
days, its implementation of a dynamic array was thus:
Declare a TYPE that is [0..MAX] of some datatype (or just a pointer if
you want).
Then you new an array of just the size that you want, and assign it to
a variable of the type previously defined.

The type allows you to access up to the maximum limit of the declared
size, even though your actual allocation isn't that large; you need to
keep track of how large your allocation was, otherwise you risk
looking into memory that is not allocated to your dynamic array, but
by virtue of the type def, you can still compile it successfully.

...Ru

Quote
CStein wrote in message ...
>>Basically, I'm not totally sure if I'm creating dynamic arrays
correctly.
>>ATM, I define a variable as a variant, then use VarArrayCreate() to
convert
>>it to an array of the correct size.  Is this the preferred way of
using
>>dynamic arrays or is there a more efficient way?

>>Also, I'm not sure how to create a dynamic array composed of
records.  I
>>have absolutely no idea how to do this, and there doesn't appear to
be any
>>information in the manuals about it.

>If VarArrayCreate( ) is working for you, that's certainly one way to
handle
>this.

>But if you're really looking to move into records, you might take a
look at
>linked lists and TLists.

Re:Tips on dynamic arrays, and help with dynamic arrays of records


You can create an array of Records this way..
Type Myrecord = Packed Record
           name:string[128];
           streat:string:[64];
       end;
var
  Myarray:array[0..50] of Myrecord;
  // now that is a Fixed size of records.., another wayu you could do this.
{$R-}
Type
     MyArrayType = Array[0..0] of MyRecord;
var
   MyArray:^MyArrayType;
begin
   GetMem(MyArray,100*Sizeof(MyRecord)); // Space for a 100 records;
    // now to access a record..
   MyArray^[10].name := 'John Smith'; //set name in record 10.
  // when done with record space..
    FreeMem(MyArray, 100*SizeOf(MyRecord));
End;
Quote
David Mason wrote:
> Hi,

> I've been learning Delphi 3 (Pro) now for around 1 month, and I'd like to
> ask some advice.

> Basically, I'm not totally sure if I'm creating dynamic arrays correctly.
> ATM, I define a variable as a variant, then use VarArrayCreate() to convert
> it to an array of the correct size.  Is this the preferred way of using
> dynamic arrays or is there a more efficient way?

> Also, I'm not sure how to create a dynamic array composed of records.  I
> have absolutely no idea how to do this, and there doesn't appear to be any
> information in the manuals about it.

> (Please can anyone replying to this message also make a CC to my email
> address?  I don't read this newsgroup regularly)

> Any help will be much appeciated,

> Dave.

Other Threads