[fpc-pascal] Open vs dynamic arrays

Michael Van Canneyt michael at freepascal.org
Sun Apr 22 12:19:48 CEST 2012



On Sun, 22 Apr 2012, Mark Morgan Lloyd wrote:

> cobines wrote:
>> 2012/4/22 Mark Morgan Lloyd <markMLl.fpc-pascal at telemetry.co.uk>:
>>>  but I'm not sure why that works when it
>>> didn't earlier (i.e. before I'd started using array of const).
>> 
>> You said you used
>> DbgArray= array of integer
>> 
>> then I assume this declaration?
>> procedure ClrDebug(const panels: DbgArray);
>> 
>> If so the parameter is a dynamic array not open array and you cannot
>> call it with constant array:
>> 
>> var
>>   arr: DbgArray;
>> begin
>>  ClrDebug([DbgKey, DbgCode1, DbgTx]); //Won't work
>>  SetLength(arr, ...);
>>  arr[0] := ...
>>  ...
>>  ClrDebug(arr);// This works
>> end;
>> 
>> 
>> This on the other hand is an open array parameter:
>> procedure ClrDebug(const panels: array of integer);
>
> Thanks, succinct. So if I understand this correctly it's one of the very rare 
> cases where one has to write out the type of a parameter in full, rather than 
> using an earlier declaration.

It results in different declarations.

type
   DbgArray = array of integer;

procedure ClrDebug(const panels: DbgArray);

Defines a procedure that accepts a dynamic array of type DbgArray.
You cannot have "constant dynamic arrays", as they are managed on 
the heap and reference counted.

On the other hand

procedure ClrDebug(const panels: array of integer);

Defines a procedure that accepts an open array: an array of integers, 
but without length. It accepts types as

type
   SmallArray = array[1..100] of integer;
   LargeArray = array[1..10000] of integer;

And a constant array (and a dynamic array, if I'm correct) as well.

I'll see if I can add something about it in the docs.

Michael.



More information about the fpc-pascal mailing list