Packed
Belongs to : KeywordDescription
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.
Notes
Examples of unpacked alignments :
Word = 2 bytes
LongWord = 4 bytes
Single = 4 bytes
Double = 8 bytes
Example code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
type // Declare an unpacked record TDefaultRecord = Record name1 : string[4]; floater : single; name2 : char; int : Integer; end; // Declare a packed record TPackedRecord = Packed Record name1 : string[4]; floater : single; name2 : char; int : Integer; end; var defaultRec : TDefaultRecord; packedRec : TPackedRecord; begin ShowMessage('Default record size = '+IntToStr(SizeOf(defaultRec))); ShowMessage('Packed record size = '+IntToStr(SizeOf(packedRec))); end; //You should see two messages like bellow Default record size = 20 Packed record size = 14 |