[fpc-pascal] about dynamic array
Michael Van Canneyt
michael at freepascal.org
Thu May 6 12:00:37 CEST 2010
On Thu, 6 May 2010, spir ☣ wrote:
> Hello,
>
>
>
> Where can one find information on types like dynamic array? Also, on funcs
> or procs that apply on them, like setLength & copy. If the answers to the
> following questions are somewhere, just tell me...
>
> * Can one rely on the fact that setLength keeps existing data (in the range of the new size), both when up- and down-sizing? Or do my trials work only because the pointed memory has not yet been overwritten?
Yes, one can rely on this.
>
> * How is resizing actually performed (is the data moved to a newly allocated area)?
This is an implementation detail which should be of no concern to the user.
>
> * Does copy onto a dynamic array automatically resize (setLength), even if the target is greater than needed? (I would like no.)
>
>
> * Can one set the first index where to copy on the target array? (eg to join 2 arrays into a greater one, or to extend an existing one that has free space at tail)
>
> * Can one use copy from/to a *static* array? (If no, why not?)
What does 'copy onto' mean in these questions ?
>
> * What other differences with static array, if any? (Aside the fact that one does not need to explicitely reallocate and copy on resizing.)
A dynamic array is reference counted.
Thus
A:=B;
Does not actually copy the entire array, it just lets B point to the same
memory area where the array data is stored and increases the reference count.
With static arrays
A:=B;
Will actually create a copy.
> For Christmas:
>
> * Can one add or remove an element --at the end? (And have the array behave accordingly.)
No.
>
> * Can one add or remove an element --anywhere? (And have the array behave accordingly.)
No.
>
> If the answers to last 2 questions is "only every 29th of februar", how
> can one have a "flexible" array? Is there something like that in stock?
> Would you implement it on top of dynamic array, or rather from scratch on
> top of static array (since the "dynamicity" does not seem very helpful)?
If you want a flexible array, use TList or TFPList or TCollection from the classes unit.
Michael.
More information about the fpc-pascal
mailing list