如何判断Delphi对齐我的记录字段的偏移量

时间:2013-09-15 00:23:03

标签: delphi memory-alignment

根据{$A}设置和Delphi版本,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);

是否有一个函数会使用某些编译器魔法为我填补偏移量?

1 个答案:

答案 0 :(得分:-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