Packed
│
Deutsch (de) │
English (en) │
русский (ru) │
The reserved word packed
tells the compiler to use as little memory as possible for a particular complex data type.
Without specifying packed
, the compiler may insert extra unused bytes between members in order to align the data on full word boundaries for faster access by the CPU.
program packedDemo(input, output, stderr);
type
signedQword = record
value: qword;
signum: -1..1;
end;
signedQwordPacked = packed record
value: qword;
signum: -1..1;
end;
begin
writeLn(' signedQword: ', sizeOf(signedQword), 'B');
writeLn('signedQwordPacked: ', sizeOf(signedQwordPacked), 'B');
end.
So packed
sacrifices some speed while reducing the memory used.
Circumvention
Smart ordering of record elements can mitigate the issue.
In the example above signedQword
as well signedQwordPacked
have signum
located at an offset of 8
.
That is OK.
If for any reason value
and signum
were listed in reverse order, signum
would appear in both versions at an offset of 0
, but in the packed
version value
has an offset of 1
Byte.
This might cause a potentially slower access to the value
field.