【问题标题】:How to tell at which offset Delphi aligns my record fields [duplicate]如何判断Delphi在哪个偏移处对齐我的记录字段[重复]
【发布时间】:2013-09-19 09:41:23
【问题描述】:

Delphi 可以对齐字、双字和四字边界上的记录,这取决于{$A} 设置和 Delphi 的版本。

如果我必须遵循(错误的)代码:

  ofSize = $00;       <<-- hardcoded will break if I unpack the record.     
  ofMSB = $01;
  ofPtrDigits = $02;
  ofSign = $06;                 

  MinSizeBigint: Byte = 10;

type
  TBigint = packed record   
    Size: Byte;
    MSB: Byte;
    PtrDigits: Pointer;
    Sign: TSignValue;

我如何把它变成这样:

type
  TBigint = record 
    PtrDigits: Pointer;  (*should be `array of cardinal`, but never mind that*)  
    Size: Byte;
    MSB: Byte;  
    Sign: TSignValue;

ofSize = OffsetOf(TBigInt.Size);    <<-- does a function like this exist?       
ofMSB = OffsetOf(TBigInt.Size);
ofPtrDigits = OffsetOf(TBigInt.Size);
ofSign = OffsetOf(TBigInt.Size);

是否有一个函数可以使用一些编译器魔法为我填充偏移量?

【问题讨论】:

标签: delphi memory-alignment


【解决方案1】:

解决方法,但不是真正的答案...

替换此代码:

....
@Exit:
  mov   byte ptr [ebx+ofMsb], cl  <<-- hardcoded offset, only works with 
  mov   dword ptr [edi+edx*4], 1       `packed record`   
....

有了这个

@Exit:
  mov   byte ptr [ebx+TBigint.MSB], cl  <<-- Delphi will put the correct offset
  mov   dword ptr [edi+edx*4], 1

【讨论】:

    猜你喜欢
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 2012-02-05
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    • 2013-06-10
    相关资源
    最近更新 更多