I have next code:
type TRecord1 = record
myarr: array [0..31] of single:
end;
type TRecord2 = record
b1, b2, b3, b4, b5, b6: byte;
end;
type TRecord3 = record
myarr: array [0..31] of single:
b1, b2, b3, b4, b5, b6: byte;
end;
procedure TForm1.FormCreate(Sender: Tobject);
begin
ShowMessage(IntToStr(SizeOf(TRecord1))+'+'+IntToStr(SizeOf(TRecord2))+
'='+IntToStr(SizeOf(TRecord3)));
end;
The program shows the following message:
128+6=136
Why is SizeOf(TRecord3) equal to 136 rather than 134?
This is due to padding added because of record alignment.
TRecord3has alignment of 4 since it containssinglevalues. And so padding is added to the end of the record to make the size an exact multiple of 4. That’s why the size is 136 rather than the value of 134 that you were expecting.You can declare your record to be
packed, or, equivalently, set the alignment compiler option to$ALIGN 1. With an alignment of1there will be no padding added to the record andSizeOf(TRecord3)=134. However, I strongly recommend you do not do this. Using the natural alignment results in the most efficient memory access for records. For example, it is more expensive for the processor to load a misaligned value than to load an aligned value. For asingleor aninteger, the natural alignment is on a 4 byte bounday. For adoublethe natural alignment is on an 8 byte boundary and so on. You should use packed records if you need binary compatibility with another library that uses packed records.