[fpc-devel]Bugs in setlength for multidimensional dynamic arrays: first partial fix

Jonas Maebe jonas at zeus.rug.ac.be
Mon Sep 24 14:06:00 CEST 2001


On maandag, september 24, 2001, at 01:56 , Michael Van Canneyt wrote:

>> var
>>    a: array of longint;
>>
>>
>> then a[<some index>] has always longint as return type.
>
> It should have. What else should it have ?
>
> If you want to declare a multidimensional array, you should declare it
> explicitly as
>
> a : array of array of longint;

I was confused because we currently accept

var
   a: array of longint;

begin
   setlength(a,10,10);
end.

> See the delphi manual (Multidimensional dynamic arrays)

I don't have delphi :)

> a : array of longint
>
> is always a one-dimensional array.

That should make things slightly easier :)

>> I don't
>> really know how to fix this cleanly in the compiler, since in some 
>> cases
>> you need "array of longint" as result type when using a[0], and in 
>> other
>> cases you need "longint".
>
> That is wrong;
> Assuming the original declaration of a, a[1] is ALWAYS of type longint.
>
> One issue is that, assuming the declaration
>
> a : array of array of longint;
>
> The following are valid calls to setlength:
>
> Setlength(A,10,20)  (10 arrays of each 20 longints)
> Setlength(A,10);    (10 unallocated arrays of longint)
> Setlength(A[10],20) (set length of 10th array to 20)

I haven't tested them all, but even the following program already 
terminates with an RTE 216 (and gives wrong output):

***
var a: array of array of longint;

begin
   setlength(a,10,5);
   writeln(high(a));
   writeln(high(a[1]));
end.
***


Jonas




More information about the fpc-devel mailing list