[fpc-pascal] about dynamic array

Jonas Maebe jonas.maebe at elis.ugent.be
Thu May 6 12:00:21 CEST 2010

On 06 May 2010, at 11:39, spir ☣ wrote:

> Where can one find information on types like dynamic array? Also, on  
> funcs or procs that apply on them, like setLength & copy.

In the documentation: http://www.freepascal.org/docs-html/ref/refsu14.html

> * Can one rely on the fact that setLength keeps existing data (in  
> the range of the new size), both when up- and down-sizing?


> * How is resizing actually performed (is the data moved to a newly  
> allocated area)?

That's implementation-defined.

> * Does copy onto a dynamic array automatically resize (setLength),  
> even if the target is greater than needed? (I would like no.)

Copy() creates a copy of the original dynamic array. If you assign the  
result afterwards to another dynamic array, that other dynamic array  
is finalised. The copy() operation itself has no idea about what you  
will do with the result.

> * 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)

See above.

> * Can one use copy from/to a *static* array? (If no, why not?)

No, simply because copy() is only defined for dynamic arrays.

> * What other differences with static array, if any? (Aside the fact  
> that one does not need to explicitely reallocate and copy on  
> resizing.)

Please read the monster thread that I just closed, rather than  
starting it all over again.

> For Christmas:
> * Can one add or remove an element --at the end? (And have the array  
> behave accordingly.)

Use setlength to increase/decrease the length by one and add/

> * Can one add or remove an element --anywhere? (And have the array  
> behave accordingly.)

Not trivially/efficiently. In general, if you want to do something  
like that you should use a linked list rather than an array. Arrays  
are notoriously inefficient (even if you use plain static arrays with  
move() and other low level operations) if you often have to insert/ 
delete elements in the middle.

> 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)?

Don't use a hammer to tighten screws.


More information about the fpc-pascal mailing list