如何调整为WINx86编译用于WINx64的代码(How to adjust the code made for WINx86 to compile for WINx64)

编程入门 行业动态 更新时间:2024-10-27 04:24:00
如何调整为WINx86编译用于WINx64的代码(How to adjust the code made for WINx86 to compile for WINx64)

我正在使用Component Tag(NativeInt)值的赋值将它添加到字节集合中。 该程序为WIN32编译时正常工作,但不能编译为WINx64。 (需要错误2001序号类型)这是一个MCVE:

program Project1; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, System.Classes; var S:set of byte; C:TComponent; begin C:=TComponent.Create(nil); C.Tag:=1; s:=[C.Tag]; C.Free; end.

如何调整代码以使其适用于WINx64编译?

I was using assignment of Component Tag (NativeInt) value adding it to the set of bytes. The program worked properly when compiled for WIN32 but doesn't compile for WINx64. (Error 2001 Ordinal type required) Here is an MCVE:

program Project1; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, System.Classes; var S:set of byte; C:TComponent; begin C:=TComponent.Create(nil); C.Tag:=1; s:=[C.Tag]; C.Free; end.

How can I adjust the code to make it suitable for WINx64 compilation?

最满意答案

Tag是NativeInt 。 这是x86中的32位和64位中的64位。 据我所知,一个32位整数被认为是一个序数,而一个64位整数不是一个序数。 这是编译器错误的原因,我认为,尽管为什么Integer被认为是序数类型,而Int64不是,我不能告诉你。 如果我不得不猜测,我会想象这与Int64不适合x86上的寄存器这一事实有关,因此与1,2和4字节序数类型相比,需要与编译器完全不同的处理方式。

既然你可能不想要改变大小的东西,我希望你可以投入Integer :

s := [Integer(C.Tag)];

而且由于您只使用低8位,所以您应该将其转换为Byte :

s := [Byte(C.Tag)];

最好有一个断言,你在范围内:

Assert((C.Tag >= low(Byte)) and (C.Tag <= high(Byte)))

坦率地说,在我看来,你完全避免了Tag 。 将数据存储在专用于该任务的变量中,并使用您可以选择的类型。 作为一般规则,在我看来, Tag是你应该避免使用的东西。 除非你碰巧有一个NatoiveInt存储,它的名字没有表明它的内容,并且当多方试图使用它时都很容易发生冲突。

Tag is NativeInt. That is 32 bits in x86, and 64 bits in x64. As I understand it, a 32 bit integer is deemed to be an ordinal, and a 64 bit integer is not an ordinal. That is the reason for the compiler error, I think, although quite why Integer is deemed to be an ordinal type, and Int64 is not, I cannot tell you. If I had to guess, I would imagine that this is related to the fact that Int64 does not fit into a register on x86, and so requires quite different treatment from the compiler in comparison to 1, 2 and 4 byte ordinal types.

Since you probably don't want something that changes size, I expect you are fine to cast to Integer:

s := [Integer(C.Tag)];

And since you are only using the low 8 bits anyway, you should probably cast it to Byte:

s := [Byte(C.Tag)];

Preferably with an assertion that you are in range:

Assert((C.Tag >= low(Byte)) and (C.Tag <= high(Byte)))

Frankly though, you are, in my view, better avoiding Tag altogether. Store your data in a variable dedicated to the task, with a type that you can choose. As a general rule, in my view, Tag is something that you should avoid using. It's not type safe unless you happen to have a NatoiveInt to store, its name gives no indication of its contents, and it's all to easy to have collisions when multiple parties attempt to use it.

更多推荐

本文发布于:2023-07-21 09:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1209017.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   compile   code   adjust

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!