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

Michael Van Canneyt michael.vancanneyt at wisa.be
Mon Sep 24 13:56:44 CEST 2001

On Mon, 24 Sep 2001, Jonas Maebe wrote:

> On woensdag, september 12, 2001, at 10:00 , Pavel V. Ozerski wrote:
> > I tried to find causes of buggy behavior of the last 1.1 compiler if it
> > tries to compile source with
> > multidimensional dynamic arrays. The problem is not fully fixed but at
> > least after my fix compiler does not
> > reject SetLength with more than two parameters (but it works
> > incorrectly then). My solution seems to be not a hack
> > but a typical bugfix.
> You're correct, I've applied your fix. It seems however that before
> multiple dimensions are supported properly for dynamical arrays, a lot
> of things stil have to be changed in the compiler :( Currently, if you
> declare
> 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;

See the delphi manual (Multidimensional dynamic arrays)

a : array of longint

is always a one-dimensional array.

> This means that
> a[1,1] won't be accepted, and high(a[1]) returns maxlongint.

This is correct and as it should be.

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


More information about the fpc-devel mailing list