Compacts complex data types into minimal storage
  type Name = Packed array[...] of ...;
  type Name = Packed class  ... end;
  type Name = Packed object ... end;
  type Name = Packed record ... end;
The Packed keyword tells Delphi to minimise the storage taken up by the defined object.
Normally, complex data types, such as records have their elements aligned to 2, 4 or 8 byte boundaries, as appropriate to the data type. For example, a Word field would be aligned to a 4 byte boundary.
Records are also padded to ensure that they end on a 4 byte boundary.
These alignments ensure optimal access performance.
The Packed overrides this, compressing the data into the smallest storage, albeit with consequential reduced access performance.
Examples of unpacked alignments :

Word     = 2 bytes
LongWord = 4 bytes
Single   = 4 bytes
Double   = 8 bytes

Related commands
$Align Determines whether data is aligned or packed
Array A data type holding indexable collections of data
Class Starts the declaration of a type of object class
Object Allows a subroutine data type to refer to an object method
Record A structured data type - holding fields of data
 Author links

 Download this web site as a Windows program.

Example code : Packing a record to reduce storage
  // Declare an unpacked record
  TDefaultRecord = Record
    name1   : string[4];
    floater : single;
    name2   : char;
    int     : Integer;

  // Declare a packed record
  TPackedRecord = Packed Record
    name1   : string[4];
    floater : single;
    name2   : char;
    int     : Integer;

  defaultRec : TDefaultRecord;
  packedRec  : TPackedRecord;

  ShowMessage('Default record size = '+IntToStr(SizeOf(defaultRec)));
  ShowMessage('Packed record size = '+IntToStr(SizeOf(packedRec)));
Show full unit code
   Default record size = 20
   Packed record size = 14
'); document.write('
'); document.write('
Delphi Programming Neil Moffatt 2002 - 2020. All rights reserved.  |  Home Page