سؤال

I have a DLL project, that provides a type definition with a Delphi "variant part" as follows:

type TValue = record 
case Kind : cardinal of 
valueShortCard : ( ValShortCard : byte ); 
valueLongReal : ( ValLongReal : double ); 
end; 

I expect that this structure would have a size of 12 bytes (4B for cardinal plus 8B for double, as this is the bigger of those two types).

But if I have an application and call the function sizeof() for the structure in the DLL, it states the size is 16 bytes.

Also, if I declare the same structure directly in the .exe application project, the sizeof() returns a correct size of 12 bytes.

If I run the application and try to get a ValLongReal value from the compiled DLL, the returned value is incorrect and it seems it is shifted by 4 bytes in the memory.

I.E. if the "double" variable in the DLL bares the value of "40 45 9a e1 47 ae 14 7b" (interpreted in byte values) , the value returned by the application after reading it from the DLL is "47 ae 14 7b 77 07 06 7d". That means there is a 4 byte overlap of correct data and the latter 4 bytes are incorrect and is read from the following space in memory.

Note: This behaviour has been observed already in Delphi XE4, we have just upgraded to XE5, but it behaves the same way. The used OS is Win7 32-bit.

thanks for your suggestions

هل كانت مفيدة؟

المحلول

Define it as such:

type TValue = packed record 
  case Kind : cardinal of 
  valueShortCard : ( ValShortCard : byte ); 
  valueLongReal : ( ValLongReal : double ); 
  end; 

Here, sizeof(TValue)=12, as you expected.

Adding packed record force no alignment to be generated. If there is no alignment, double will be aligned on 8 bytes boundaries, so Kind will use 8 bytes instead of 4, and valueLongReal also, therefore, sizeof(TValue)=8+8=16 in your case.

Note that under AMD/Intel, you should better align your double variables for performance reasons. On ARM, alignment is required.

Note also that variable records alignment changed with Delphi XE3 AFAIR in respect to previous versions.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top