关于 Delphi 和 c++ 中的枚举在 64 位环境中
我最近不得不解决 Delphi 和 C++ 中用于枚举的不同默认大小,因为我必须使用 Delphi 应用程序中的 C++ dll。
一个函数调用返回一个结构数组(或 delphi 中的记录),其第一个元素是枚举。
为了完成这项工作,我使用打包记录(或对齐(1)结构)。但是,由于 delphi 默认情况下动态选择枚举变量的大小并使用可能的最小数据类型(在我的例子中是字节),但 C++ 使用 int 进行枚举,因此我的数据无法正确解释。
Delphi 提供了一个编译器开关来解决这个问题,因此枚举的声明变成了
{$Z4}
TTypeofLight =
(
V3d_AMBIENT,
V3d_DIRECTIONAL,
V3d_POSITIONAL,
V3d_SPOT
);
{$Z1}
我的问题是:
- 当我的结构在 64 位环境上编译时会变成什么?
- 默认的 C++ 整数会增长到 8 字节吗?
- 是否还有其他内存对齐/数据类型大小修改(指针除外)?
I recently had to work around the different default sizes used for enumerations in Delphi and c++ since i have to use a c++ dll from a delphi application.
One function call returns an array of structs (or records in delphi), the first element of which is an enum.
To make this work, I use packed records (or aligned(1)-structs). However, since delphi selects the size of an enum-variable dynamically by default and uses the smallest datatype possible (it was a byte in my case), but C++ uses an int for enums, my data was not interpreted correctly.
Delphi offers a compiler switch to work around this, so the declaration of the enum becomes
{$Z4}
TTypeofLight =
(
V3d_AMBIENT,
V3d_DIRECTIONAL,
V3d_POSITIONAL,
V3d_SPOT
);
{$Z1}
My Questions are:
- What will become of my structs when they are compiled on/for a 64-bit environment?
- Does the default c++ integer grow to 8 Bytes?
- Are there other memory alignment / data type size modifications (other than pointers)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当 Delphi 支持 64 位编译时,整数大小将保持 4 字节,指针增加到 8。FreePascal 有 PtrInt 和 PtrUInt 类型,它们是“足够大的整数来容纳指针”,Delphi 可能会添加类似的东西。
由于您明确声明了枚举大小,即使您所在的平台整数大小增加,它们也将保持 4 字节,因为通常它们仅为 1 字节。布局解压数据结构时,将应用现有规则,字段根据类型的大小进行对齐,因此整数将是 4 字节对齐,Int64 和指针将是 8 字节对齐。
维基百科有一个表格,显示各种 64 位操作系统的数据类型大小,但现在推断 Embarcadero 将如何处理其他 64 位平台(Linux 和 OS X)还为时过早。
When Delphi supports 64-bit compilation the integer size will remain 4-bytes, with pointers increasing to 8. FreePascal has PtrInt and PtrUInt types which are "Integers large enough to hold pointers" and Delphi will presumably add something similar.
Since you're explicitly declaring the enumeration sizes they will remain 4-bytes even if you were on a platform where the integer size increased, since normally they would only be 1 byte. When laying out unpacked data structures the existing rule will apply, that fields are aligned based on the type's size, so integers will be 4-byte aligned, and Int64 and Pointers will be 8-byte aligned.
Wikipedia has a table showing data type sizes for various 64-bit OSes, but it's too early to extrapolate how Embarcadero will handle other 64-bit platforms (Linux and OS X).
Delphi 没有 64 位编译器,因此您无法编译 64 位程序。但是,您仍然可以编译它并在 64 位操作系统上将其作为 32 位进程运行。在这种情况下,你的结构将会发生注意到。
库的问题有点复杂:如果将其编译为 64 位库,则根本无法将其加载到 32 位进程中。但是,假设您要编译 64 位,然后在 64 位进程中使用它,那么 int 变量的实际长度很可能保持 32 位(并非所有情况都是如此,尽管)。
请参阅此维基百科文章了解更多信息:
http://en.wikipedia.org/wiki /64 位#Specific_data_models
There is no 64bits compiler for Delphi so you can't compile your program for 64bits. However, you can still compile it and run it on a 64bit OS as a 32 bits process. in that case, noting will happens to your structures.
The question of the library is a bit more complex: if you compile it as a 64 bits library, you won't be able to load it in your 32-bits process at all. However, assuming you're going to compile it for 64 bits and then use it from a 64 bits process, then the actual length of an int variable is most likely going to stay 32 bits (that's not going to be the case for everything, though).
See this wikipedia article for some more information:
http://en.wikipedia.org/wiki/64-bit#Specific_data_models