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.

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 typesl

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.

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.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment