在64枚举集大小 [英] Enumeration set size in x64
问题描述
我发现,一下SizeOf(套),我在32位和64位的不同,下面的例子显示了5个字节为32位和8对64位。但我发现了大约64位改变一下SizeOf(套)什么信息。是否有任何Embarcadero的文档有关,或编译器指令来获得32位和64位。类似的结果。
程序PROJECT1;
{$ APPTYPE CONSOLE}
{$ R * .RES}
使用System.SysUtils;
类型
{属性枚举}
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 =集TProperty1的;
开始
WriteLn(一下SizeOf(TProperties1));
ReadLn;
结束。
要回答你的问题。我无法找到有关的分歧或编译器指令去改变的英巴卡迪诺网站任何内容。我的研究表明如下:
设定在字节以下大小的32位:
- 最多8个元素 - 1字节
- 在9至16个元素 - 2字节
- 在17到32个元素 - 4字节
从这时开始它增加了在同一时间增加了字节根据需要之一。因此,33〜40元使用5个字节,41至48 Elements使用6个字节。
在64位模式下,情况略有不同:
- 最多8个元素 - 1字节
- 在9至16个元素 - 2字节
- 在17到32个元素 - 4字节
- 在33到64个元素 - 8字节
从这时开始它增加了在同一时间增加了字节根据需要之一。因此,65〜72元使用9个字节,73至80元使用10字节。
要解决这个问题,你要需要或者使用类似写集
在 TWriter.WriteProperty
和 TReader.ReadSet
或者你可以做这样的事情:
程序SaveSetToStream(aStream:TStream;常量ASET:TProperties1);
变种
streamData:数组[0..7]字节;
开始
断言(一下SizeOf(ASET)< =一下SizeOf(streamData),设置过大,保存增加数组的长度。);
FillChar(streamData,一下SizeOf(streamData),0);
移动(ASET,streamData,一下SizeOf(ASET));
aStream.Write(streamData,一下SizeOf(streamData));
结束;
功能ReadFromStream(aStream:TStream):TProperties1;
变种
streamData:数组[0..7]字节;
开始
断言(一下SizeOf(结果)< =一下SizeOf(streamData),设置过大,负荷增加了数组的长度。);
aStream.Read(streamData,一下SizeOf(streamData));
移动(streamData,结果,一下SizeOf(结果));
结束;
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.
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:
Sets have the following sizes in bytes in 32 bit:
- Up to 8 elements - 1 Byte
- 9 to 16 elements - 2 Bytes
- 17 to 32 elements - 4 Bytes
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.
In 64 bit mode, things are slightly different:
- Up to 8 elements - 1 Byte
- 9 to 16 elements - 2 Bytes
- 17 to 32 elements - 4 Bytes
- 33 to 64 elements - 8 Bytes
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.
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;
这篇关于在64枚举集大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!