I'm trying to figure out what a conformant array is, but I can't find a good definition anywhere. Can anyone explain what this is and give me a basic example, and what the purpose of them is?
A Conformant Array isn't a dynamic array that resizes itself to fit data, like Java's List class. It's a mechanism that lets Pascal procedures and functions work with variable-sized arrays.
In Pascal, the size of an array is part of its type, so in:
VAR
MyArray1 : ARRAY [1..10] OF INTEGER;
MyArray2 : ARRAY [1..20] OF INTEGER;
MyArray1
and MyArray2
are of two different types and aren't compatible.
This becomes a problem with procedures/functions in classic Pascal: because the formal parameters of a procedure must have a specific type, and because an array's length is part of its type, it is in general not possible to define a procedure/function that works on arrays of arbitrary lengths.
Conformant Arrays were introduced as part of ISO Standard Pascal to work around this issue by allowing a procedure/function to be defined taking a "conformant array" as a parameter, like this:
PROCEDURE MyProc(VAR x : ARRAY [low..high : INTEGER] OF INTEGER);
This defines procedure MyProc
with three formal parameters: x
, low
, and high
(i.e. there are three parameters being passed in of arbitrary name). When the procedure is invoked, x
is set to a pointer to the array, low
is an integer variable set to the lower bound of the array and high
is an integer variable set to the upper bound of the array.
This mechanism allows array bounds to be passed with the array in a type-safe manner, and lets procedures work with arrays of arbitrary size. It's a feature defined by ISO Standard Pascal, but other Pascals (e.g. Extended Pascal and Borland Pascal) came up with different mechanisms to deal with this problem (e.g. Extended Pascal's type schemas, which are much richer and more complicated).