Name

Packed Keyword

Syntax

type Name = packed record ... end;
type Name = packed class ... end;
type Name = packed object ... end;
type Name = packed array[...] of ...;

Description

Delphi aligns record and object fields on natural boundaries to improve performance. If you use the packed directive, Delphi does not insert any padding to align fields within the object or record.

An aligned record is also padded so its size is a multiple of 4 bytes. A packed record does not have any extra padding, but Delphi aligns and pads variables and dynamically allocated memory on 4-byte boundaries, so the size of a packed record is meaningful only if you are creating an array of records or using records as fields in other structured types.

The unpacked alignment for a field depends on the size of the field:

  • Byte-sized fields and sets of any size are aligned on byte boundaries.

  • Word-sized fields are aligned on word (2-byte) boundaries.

  • LongWord-sized fields (including Single) are aligned on long word (4-byte) boundaries.

  • Other types (Comp, Currency, Double, Extended, Int64, Real48, Variant) are aligned on 8-byte boundaries.

  • Arrays are aligned according to the alignment of the array’s base type.

  • Records are aligned according to the largest alignment used by a member in the record.

Tips and Tricks

  • Accessing a packed field is slower than accessing an aligned field. Use the packed keyword only when the need to conserve memory outweighs the performance penalty.

  • Delphi does not pack Boolean fields into single ...

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.