Free Pascal supports arrays as in Turbo Pascal, multi-dimensional arrays and packed arrays are also supported, as well as the dynamic arrays of Delphi:
_________________________________________________________________________________________________________
Array types
___________________________________________________________________
When the range of the array is included in the array de nition, it is called a static array. Trying to access an element with an index that is outside the declared range will generate a run-time error (if range checking is on). The following is an example of a valid array declaration:
Type
RealArray = Array [1..100] of Real; |
Valid indexes for accessing an element of the array are between 1 and 100, where the borders 1 and 100 are included. As in Turbo Pascal, if the array component type is in itself an array, it is possible to combine the two arrays into one multi-dimensional array. The following declaration:
Type
APoints = array[1..100] of Array[1..3] of Real; |
is equivalent to the following declaration:
Type
APoints = array[1..100,1..3] of Real; |
The functions High (??) and Low (??) return the high and low bounds of the leftmost index type of the array. In the above case, this would be 100 and 1.
When static array-type variables are assigned to each other, the contents of the whole array is copied. This is also true for multi-dimensional arrays:
program testarray1;
Type TA = Array[0..9,0..9] of Integer; var A,B : TA; I,J : Integer; begin For I:=0 to 9 do For J:=0 to 9 do A[I,J]:=I*J; For I:=0 to 9 do begin For J:=0 to 9 do Write(A[I,J]:2,' '); Writeln; end; B:=A; Writeln; For I:=0 to 9 do For J:=0 to 9 do A[9-I,9-J]:=I*J; For I:=0 to 9 do begin For J:=0 to 9 do Write(B[I,J]:2,' '); Writeln; end; end. |
The output will be 2 identical matrices.
As of version 1.1, Free Pascal also knows dynamic arrays: In that case, the array range is omitted, as in the following example:
Type
TByteArray : Array of Byte; |
When declaring a variable of a dynamic array type, the initial length of the array is zero. The actual length of the array must be set with the standard SetLength function, which will allocate the memory to contain the array elements on the heap. The following example will set the length to 1000:
Var
A : TByteArray; begin SetLength(A,1000); |
After a call to SetLength, valid array indexes are 0 to 999: the array index is always zero-based.
Note that the length of the array is set in elements, not in bytes of allocated memory (although these may be the same). The amount of memory allocated is the size of the array multiplied by the size of 1 element in the array. The memory will be disposed of at the exit of the current procedure or function.
It is also possible to resize the array: in that case, as much of the elements in the array as will t in the new size, will be kept. The array can be resized to zero, which e ectively resets the variable.
At all times, trying to access an element of the array that is not in the current length of the array will generate a run-time error.
Assignment of one dynamic array-type variable to another will let both variables point to the same array. Contrary to ansistrings, an assignment to an element of one array will be re ected in the other:
Var
A,B : TByteArray; begin SetLength(A,10); A[1]:=33; B:=A; A[1]:=31; |
After the second assignment, the rst element in B will also contain 31.
It can also be seen from the output of the following example:
program testarray1;
Type TA = Array of array of Integer; var A,B : TA; I,J : Integer; begin Setlength(A,10,10); For I:=0 to 9 do For J:=0 to 9 do A[I,J]:=I*J; For I:=0 to 9 do begin For J:=0 to 9 do Write(A[I,J]:2,' '); Writeln; end; B:=A; Writeln; For I:=0 to 9 do For J:=0 to 9 do A[9-I,9-J]:=I*J; For I:=0 to 9 do begin For J:=0 to 9 do Write(B[I,J]:2,' '); Writeln; end; end. |
The output will be a matrix of numbers, and then the same matrix, mirrorred.
Dynamic arrays are reference counted: if in one of the previous examples A goes out of scope and B does not, then the array is not yet disposed of: the reference count of A (and B) is decreased with 1. As soon as the reference count reaches zero, the memory is disposed of.
It is also possible to copy and/or resize the array with the standard Copy function, which acts as the copy function for strings:
program testarray3;
Type TA = array of Integer; var A,B : TA; I : Integer; begin Setlength(A,10); For I:=0 to 9 do A[I]:=I; B:=Copy(A,3,9); For I:=0 to 5 do Writeln(B[I]); end. |
The Copy function will copy 9 elements of the array to a new array. Starting at the element at index 3 (i.e. the fourth element) of the array.
The Low function on a dynamic array will always return 0, and the High function will return the value Length-1, i.e., the value of the highest allowed array index. The Length function will return the number of elements in the array.