views:

251

answers:

2

I recently had to work around the different default sizes used for enumerations in Delphi and c++ since i have to use a c++ dll from a delphi application.

One function call returns an array of structs (or records in delphi), the first element of which is an enum.

To make this work, I use packed records (or aligned(1)-structs). However, since delphi selects the size of an enum-variable dynamically by default and uses the smallest datatype possible (it was a byte in my case), but C++ uses an int for enums, my data was not interpreted correctly.

Delphi offers a compiler switch to work around this, so the declaration of the enum becomes

    {$Z4} 
    TTypeofLight = 
    (
        V3d_AMBIENT,
        V3d_DIRECTIONAL,
        V3d_POSITIONAL,
        V3d_SPOT
   );
   {$Z1}

My Questions are:

  • What will become of my structs when they are compiled on/for a 64-bit environment?
  • Does the default c++ integer grow to 8 Bytes?
  • Are there other memory alignment / data type size modifications (other than pointers)?
+2  A: 

There is no 64bits compiler for Delphi so you can't compile your program for 64bits. However, you can still compile it and run it on a 64bit OS as a 32 bits process. in that case, noting will happens to your structures.

The question of the library is a bit more complex: if you compile it as a 64 bits library, you won't be able to load it in your 32-bits process at all. However, assuming you're going to compile it for 64 bits and then use it from a 64 bits process, then the actual length of an int variable is most likely going to stay 32 bits (that's not going to be the case for everything, though).

See this wikipedia article for some more information:

http://en.wikipedia.org/wiki/64-bit#Specific_data_models

Stephane
Thank you for the input. +1
sum1stolemyname
(there are some mainframe unices that are ILP64, iow integer=64-bit, but you are unlikely to run into them using plain Delphi)
Marco van de Voort
+3  A: 

When Delphi supports 64-bit compilation the integer size will remain 4-bytes, with pointers increasing to 8. FreePascal has PtrInt and PtrUInt types which are "Integers large enough to hold pointers" and Delphi will presumably add something similar.

Since you're explicitly declaring the enumeration sizes they will remain 4-bytes even if you were on a platform where the integer size increased, since normally they would only be 1 byte. When laying out unpacked data structures the existing rule will apply, that fields are aligned based on the type's size, so integers will be 4-byte aligned, and Int64 and Pointers will be 8-byte aligned.

Wikipedia has a table showing data type sizes for various 64-bit OSes, but it's too early to extrapolate how Embarcadero will handle other 64-bit platforms (Linux and OS X).

Craig Peterson
Delphi already has IntPtr and UIntPtr for these types.
Barry Kelly
FPC doesn't seem to support 8 byte enums (e.g. using $packenum) either: Error: Illegal enum minimum-size specifier "8".(btw: IntPtr was only added in later Delphi versions way after FPC added PtrInt (2005 timeframe), hence the difference. )
Marco van de Voort