A structured type is a type that can hold multiple values in one variable. Stuctured types can be nested to unlimited levels.
Structured Types
Unlike Delphi, Free Pascal does not support the keyword Packed for all structured types, as can be seen in the syntax diagram. It will be mentioned when a type supports the packed keyword.
In the following, each of the possible structured types is discussed.
Free Pascal supports arrays as in Turbo Pascal, multi-dimensional arrays and packed arrays are also supported:
Array types
The following is a valid array declaration:
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:
is equivalent to the following declaration:
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.
Free Pascal supports fixed records and records with variant parts. The syntax diagram for a record type is
Record types
So the following are valid record types declarations:
The variant part must be last in the record. The optional identifier in the case statement serves to access the tag field value, which otherwise would be invisible to the programmer. It can be used to see which variant is active at a certain time. In effect, it introduces a new field in the record.
Remark that it is possible to nest variant parts, as in:
The size of a record is the sum of the sizes of its fields, each size of a field is rounded up to two. If the record contains a variant part, the size of the variant part is the size of the biggest variant, plus the size of the tag field type if an identifier was declared for it. Here also, the size of each part is first rounded up to two. So in the above example, SizeOf would return 24 for Point, 24 for RPoint and 26 for BetterRPoint. For MyRec, the value would be 12.
If you want to read a typed file with records, produced by a Turbo Pascal program, then chances are that you will not succeed in reading that file correctly.
The reason for this is that by default, elements of a record are aligned at 2-byte boundaries, for performance reasons. This default behaviour can be changed with the {$PackRecords n} switch. Possible values for n are 1, 2, 4, 16 or Default. This switch tells the compiler to align elements of a record or object or class on 1,2,4 or 16 byte boundaries. The keyword Default selects the default value for the platform you're working on (currently 2 on all platforms)
Take a look at the following program:
The output of this program will be :
And this is as expected. In Trec1, each of the elements A and
B takes 2 bytes of memory, and in Trec1, A takes only 1
byte of memory.
As from version 0.9.3, Free Pascal supports also the 'packed record', this is a record where all the elements are byte-aligned.
Thus the two following declarations are equivalent:
The output of this program will be :
And this is as expected. In Trec1, each of the elements A and
B takes 2 bytes of memory, and in Trec1, A takes only 1
byte of memory.
As from version 0.9.3, Free Pascal supports also the 'packed record', this is a record where all the elements are byte-aligned.
Thus the two following declarations are equivalent:
and
Note the {$PackRecords 2} after the first declaration !
Free Pascal supports the set types as in Turbo Pascal. The prototype of a set declaration is:
Set Types
Each of the elements of SetType must be of type TargetType. TargetType can be any ordinal type with a range between 0 and 255. A set can contain maximally 255 elements.
The following are valid set declaration:
Given this set declarations, the following assignment is legal:
The operators and functions for manipulations of sets are listed in
table ().
Operation | Operator |
Union | + |
Difference | - |
Intersection | * |
Add element | include |
Delete element | exclude |
You can compare two sets with the <> and = operators, but not (yet) with the < and > operators.
As of compiler version 0.9.5, the compiler stores small sets (less than 32 elements) in a Longint, if the type range allows it. This allows for faster processing and decreases program size. Otherwise, sets are stored in 32 bytes.
File types are types that store a sequence of some base type, which can be any type except another file type. It can contain (in principle) an infinite number of elements.
File types are used commonly to store data on disk. Nothing stops you, however, from writing a file driver that stores it's data in memory.
Here is the type declaration for a file type:
If no type identifier is given, then the file is an untyped file; it can be considered as equivalent to a file of bytes. Untyped files require special commands to act on them (see Blockread, Blockwrite).
File types
The following declaration declares a file of records:
Internally, files are represented by the FileRec record.
See chapter () for it's declaration.
A special file type is the Text file type, represented by the TextRec record. A file of type Text uses special input-output routines.