x64 中的枚举集大小 [英] Enumeration set size in x64
问题描述
我发现 32 位和 64 位的 SizeOf(set) i 不同,下面的示例显示 32 位的 5 个字节和 64 位的 8 个字节.但是我没有发现有关 64 位 SizeOf(sets) 变化的信息.是否有任何关于它的 Embarcadero 文档或编译器指令可以在 32 位和 64 位上获得类似的结果.
I found that a SizeOf(set) i different in 32-bit and 64-bit, the example below shows 5 byte for 32-bit and 8 for 64-bit. But i found nothing information about changes in SizeOf(sets) for 64-bit. Is there any Embarcadero documentation about it or compiler directive to get a similar results on 32 and 64-bit.
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses System.SysUtils;
type
{ Enumeration of properties}
TProperty1 = (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14,
p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, p27, p28,
p29, p30, p31, p32, p33, p34, p35, p36, p37);
TProperties1 = set of TProperty1;
begin
WriteLn(SizeOf(TProperties1));
ReadLn;
end.
推荐答案
回答您的问题.我在 Embarcadero 网站上找不到任何关于差异或编译器指令来改变行为的信息.我的研究表明:
To answer your question. I couldn't find anything on the Embarcadero site regarding the differences or a compiler directive to change the behavior. My research indicates the following:
集合在 32 位中具有以下大小(以字节为单位):
Sets have the following sizes in bytes in 32 bit:
- 最多 8 个元素 - 1 个字节
- 9 到 16 个元素 - 2 个字节
- 17 到 32 个元素 - 4 个字节
从这一点开始,它会根据需要添加字节,一次一个.所以33到40个元素使用5个字节,41到48个元素使用6个字节.
From this point onwards it adds adds bytes as needed, one at a time. So 33 to 40 elements uses 5 bytes and 41 to 48 elements uses 6 bytes.
在 64 位模式下,情况略有不同:
In 64 bit mode, things are slightly different:
- 最多 8 个元素 - 1 个字节
- 9 到 16 个元素 - 2 个字节
- 17 到 32 个元素 - 4 个字节
- 33 到 64 个元素 - 8 字节
从这一点开始,它会根据需要添加字节,一次一个.所以65到72个元素使用9个字节,73到80个元素使用10个字节.
From this point onwards it adds adds bytes as needed, one at a time. So 65 to 72 elements uses 9 bytes and 73 to 80 elements uses 10 bytes.
为了解决这个问题,你需要在 TWriter.WriteProperty
和 TReader.ReadSet
中使用类似 WriteSet
的东西,或者你可以做这样的事情:
To get around this you are going to need to either use something like WriteSet
in TWriter.WriteProperty
and TReader.ReadSet
or you can do something like this:
procedure SaveSetToStream(aStream: TStream; const aSet: TProperties1);
var
streamData: array[0..7] of byte;
begin
Assert(SizeOf(aSet) <= SizeOf(streamData), 'Set is too large to save. Increase the array length.');
FillChar(streamData, SizeOf(streamData), 0);
Move(aSet, streamData, SizeOf(aSet));
aStream.Write(streamData, SizeOf(streamData));
end;
function ReadFromStream(aStream: TStream): TProperties1;
var
streamData: array[0..7] of byte;
begin
Assert(SizeOf(Result) <= SizeOf(streamData), 'Set is too large to load. Increase the array length.');
aStream.Read(streamData, SizeOf(streamData));
Move(streamData, Result, SizeOf(Result));
end;
这篇关于x64 中的枚举集大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!