【发布时间】:2018-04-11 19:01:48
【问题描述】:
如果我定义这样的结构 struct foo {char x; float y ; char z; double t}; 对齐可以依赖于平台,但我希望在大多数架构/编译器上成员 x、y、z、t 的偏移量分别为 0、4、8、16。
如果我在 Squeak 中定义了相应的结构
ExternalStructure subclass: #Foo
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'FFI-Tests'.
并用天真地定义字段
Foo class>fields
^#(
(x 'char')
(y 'float')
(z 'char')
(t 'double') )
然后使用Foo defineFields 生成访问器,我得到那些打包的偏移量:
Foo>>x
^handle unsignedCharAt: 1
Foo>>y
^handle floatAt: 2
Foo>>z
^handle unsignedCharAt: 6
Foo>>t
^handle doubleAt: 7
即分别为0,1,5,6的字段偏移量。
我应该如何获得与平台兼容的对齐方式?
- 插入填充字段?
- 强制以前的字段大小?
- 忘记自动生成的东西,自己编写所有访问方法?
【问题讨论】: