4

In Delphi 2009 (or older versions), how do you check the "Align" compile option in the code?

The IFOPT directive seems to work only with pure switches ( {$IFOPT A4} does not compile ).

I couldn't find an equivalent constant or such defined ( {$IF Align = 4} or such )

4 Answers 4

12

You can do this by defining a record with known packing rules and check it using SizeOf. Tested in Delphi 2009:

type
  TTestRec = record
    A: Byte;
    B: Int64;
  end;

{$IF SIZEOF(TTestRec) = 9}
  {$MESSAGE HINT '$A1'}
{$ELSEIF SIZEOF(TTestRec) = 10}
  {$MESSAGE HINT '$A2'}
{$ELSEIF SIZEOF(TTestRec) = 12}
  {$MESSAGE HINT '$A4'}
{$ELSEIF SIZEOF(TTestRec) = 16}
  {$MESSAGE HINT '$A8'}
{$ELSE}
  {$MESSAGE HINT 'Unknown alignment'}
{$IFEND}
1
  • Thanks for the answer. Do we have a rock-solid guaranty that the compiler treats in the exact same way non packed records and local variables placed on the stack? (I certainly do hope so, but I have had several surprises when testing the compiler behaviour on other features...)
    – LeGEC
    Commented May 11, 2009 at 13:01
2

Write code to test the actual runtime behavior. Only way I can think of.

1

There is {$IFOPT A+} directive, but it doesn't tell you alignment value.

-1

I believe there is no way to do this :(

5
  • I agree. I don't know of any way to do this either. (Not sure why you'd need to; I can't think of any reason you'd need to know this at compile time, or what you'd do differently in code if you did know.
    – Ken White
    Commented May 6, 2009 at 13:25
  • 2
    Certain tasks only work on properly-aligned values. A lot of SIMD instructions are like that, for example... Commented May 6, 2009 at 14:24
  • @Mason: That's right, but why not simply demand or force proper alignment, and do the same thing the processor does when misaligning, i.e. throw? Commented May 6, 2009 at 14:52
  • 1
    Trying to implement allen bauer's idea to create generic multicast events, we found several bugs, among others in ObjAuto.pas. For example, his solution implies creating at runtime a TMethodPointer from a method (and its TypeInfo), using ObjAuto.CreateMethodPointer. This function incorrectly assumes (through nested calls : line 726) that local variables and arguments are not aligned on the stack. To fix this, for example, we need to know the alignment size. Allen Bauer's original post : blogs.embarcadero.com/abauer/2008/08/15/38865#comment-11698
    – LeGEC
    Commented May 6, 2009 at 15:01
  • I'm using smart records to generate 4-aligned and 8-aligned values. See TGp4AlignedInt and TGp8AlignedInt64 in my GpStuff unit (most recent version is at code.google.com/p/omnithreadlibrary/source/browse/trunk/src/…).
    – gabr
    Commented May 6, 2009 at 21:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.