-1

Delphi can align records on word, double-word and quad-word bouderies, depending on the {$A} setting and the version of Delphi.

If I have to following (bad) code:

  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;

How do I transform it into this:

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);

Is there a function that will fill in the offsets for me using some compiler magic?

Charles
  • 50,943
  • 13
  • 104
  • 142
Johan
  • 74,508
  • 24
  • 191
  • 319
  • 1
    Quite recently discussed here: http://stackoverflow.com/questions/18786986/can-we-implement-ansi-cs-offsetof-in-delphi – Sertac Akyuz Sep 15 '13 at 01:53

1 Answers1

-1

Workaround, but not the real answer...

Replace this code:

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

With this

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