Quick start manual

11-8
Delphi Language Guide
Internal data formats
Record types
When a record type is declared in the {$A+} state (the default), and when the
declaration does not include a packed modifier, the type is an unpacked record type,
and the fields of the record are aligned for efficient access by the CPU. The alignment
is controlled by the type of each field and by whether fields are declared together.
Every data type has an inherent alignment, which is automatically computed by the
compiler. The alignment can be 1, 2, 4, or 8, and represents the byte boundary that a
value of the type must be stored on to provide the most efficient access. The table
below lists the alignments for all data types.
To ensure proper alignment of the fields in an unpacked record type, the compiler
inserts an unused byte before fields with an alignment of 2, and up to three unused
bytes before fields with an alignment of 4, if required. Finally, the compiler rounds
the total size of the record upward to the byte boundary specified by the largest
alignment of any of the fields.
If two fields share a common type specification, they are packed even if the
declaration does not include the packed modifier and the record type is not declared
in the {$A–} state. Thus, for example, given the following declaration
type
TMyRecord = record
A, B: Extended;
C: Extended;
end;
A and B are packed (aligned on byte boundaries) because they share the same type
specification. The compiler pads the structure with unused bytes to ensure that C
appears on a quadword boundary.
When a record type is declared in the {$A–} state, or when the declaration includes
the packed modifier, the fields of the record are not aligned, but are instead assigned
consecutive offsets. The total size of such a packed record is simply the size of all the
fields. Because data alignment can change, it's a good idea to pack any record
structure that you intend to write to disk or pass in memory to another module
compiled using a different version of the compiler.
Table 11.4 Type alignment masks
Type Alignment
Ordinal types size of the type (1, 2, 4, or 8)
Real types 2 for Real48, 4 for Single, 8 for Double and Extended
Short string types 1
Array types same as the element type of the array.
Record types the largest alignment of the fields in the record
Set types size of the type if 1, 2, or 4, otherwise 1
All other types determined by the $A directive.