type
Ta= record
a : Extended;
end;
begin
ShowMessage(IntToStr(SizeOf(Extended)));
ShowMessage(IntToStr(SizeOf(Ta)));
end;
Extended类型占10字节,但为何Ta却是16字节?
Ta= record
a : Extended;
end;
begin
ShowMessage(IntToStr(SizeOf(Extended)));
ShowMessage(IntToStr(SizeOf(Ta)));
end;
Extended类型占10字节,但为何Ta却是16字节?
ta=packed record
x:extended;
end;
这样就是10字节
a : Extended;
end;
Ta是 record 类型啊, 它应该还要开内存,存储一些东西,如,Ta 中有几个成员,等等
record为了优化寻址速度,会把成员按4字节进行对齐(猜的)
但是:
Ta=record
a:Extended;
end;
ShowMessage(IntToStr(SizeOf(Ta)));//结果为16,证明成员为一个时也会对齐。而且以8字节为单位。
而
Ta=record
a:Integer;
end;
ShowMessage(IntToStr(SizeOf(Ta)));//结果为4,没有以8字节单位对齐,不知如何理解???
我试了一下以下情况
Ta=record
a:Byte;
end; Ta=record
a:Word;
end; Ta=record
a:Integer;
end; 测试下来,感觉当记录只有一个域,且该域类型占用字节小于4个字节时,就不进行对齐了。
而以下情况,因为域占用字节大于4个字节了,就进行对齐了。
Ta=record
a:Real48;
end; Ta=record
a:Extended;
end;
Type Alignment
Ordinal types size of the type (1, 2, 4, or 8)
Real types 2 for Real48, 4 for Single, 8 for Double and Extended
Short string types 1
Array types same as the element type of the array.
Record types the largest alignment of the fields in the record
Set types size of the type if 1, 2, or 4, otherwise 1
All other types determined by the $A directive.
To ensure proper alignment of the fields in an unpacked record type, the compiler inserts an unused byte before fields with an alignment of 2, and up to three unused bytes before fields with an alignment of 4, if required. Finally, the compiler rounds the total size of the record upward to the byte boundary specified by the largest alignment of any of the fields.
If two fields share a common type specification, they are packed even if the declaration does not include the packed modifier and the record type is not declared in the {$A-} state. Thus, for example, given the following declarationtype
TMyRecord = record
A, B: Extended;
C: Extended;
end;A and B are packed (aligned on byte boundaries) because they share the same type specification. The compiler pads the structure with unused bytes to ensure that C appears on a quadword boundary.When a record type is declared in the {$A-} state, or when the declaration includes the packed modifier, the fields of the record are not aligned, but are instead assigned consecutive offsets. The total size of such a packed record is simply the size of all the fields. Because data alignment can change, it's a good idea to pack any record structure that you intend to write to disk or pass in memory to another module compiled using a different version of the compiler.
==============
没有时间细细翻译了。
又如:
Ta= record
b : Char;
c : Char;
a : Integer;
end;按我的理解,以最大的成员字节数对齐,上面应该是4+4+4=12,但结果是8。不知如何理解呢
//最大字节对齐是并不是按最大字节分配内存.
type
Ta= record
b : Char;
c : Char;
a : Integer;
end;
//上述结构存放如下图所示
1 2 3 4 5 6 7 8
b c a a a a
如果改为
type
Ta= record
b : Char;
c : Char;
a : Integer;
d : Char;
end;
则存放结构为
1 2 3 4 5 6 7 8 9 10 11 12
b c a a a a d
对于此例,Extended的长度为10个字节,其对齐是要求在8个字节边界上,当然这里只有一个field,所以没有特别的对齐问题,但是当record长度不是8个字节的整数倍时,delphi还会自动将整个的record长度延长到8字节的整数倍,故为2×8=16=10+6。
长见识。
record中各field的对齐要求中最大的那个如果是8字节地址的,那么就是8的倍数;
具体与field的数据类型的关系:
Ordinal types: size of the type (1, 2, 4, or 8)
Real types: 2 for Real48, 4 for Single, 8 for Double and Extended