next up previous contents index
Next: Pointers Up: Types Previous: Character types

Structured Types

A structured type is a type that can hold multiple values in one variable. Stuctured types can be nested to unlimited levels.


Structured Types

syntdiag1447

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.

Arrays

Free Pascal supports arrays as in Turbo Pascal, multi-dimensional arrays and packed arrays are also supported:


Array types

syntdiag1489

The following is a valid array declaration:
listing1468

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:
listing1470
is equivalent to the following declaration:
listing1472

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.

Record types

Free Pascal supports fixed records and records with variant parts. The syntax diagram for a record type is


Record types

syntdiag1599

syntdiag1603

syntdiag1607

syntdiag1611

syntdiag1615

So the following are valid record types declarations:
listing1535

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:
listing1537

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:
listing1549
The output of this program will be :
listing1553
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:
listing1560
The output of this program will be :
listing1553
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:
listing1560
and
listing1564
Note the {$PackRecords 2} after the first declaration !

Set types

Free Pascal supports the set types as in Turbo Pascal. The prototype of a set declaration is:


Set Types

syntdiag1671

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:
listing1633
Given this set declarations, the following assignment is legal:
listing1635
The operators and functions for manipulations of sets are listed in table (gif).

  

Operation Operator
Union +
Difference -
Intersection *
Add element include
Delete element exclude
Table: Set Manipulation operators

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

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:


File types

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

The following declaration declares a file of records:
listing1701

Internally, files are represented by the FileRec record. See chapter (gif) 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.


next up previous contents index
Next: Pointers Up: Types Previous: Character types

Michael Van Canneyt
Fri Sep 25 09:15:40 MEST 1998