Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-01

Incompatible record alignment?

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Gheorghe Stefan BERINDE

Hello all,

I encounter some difficulties when passing a record variable from a FPC dynamic link library to a Delphi host program. I try to align the record structure on 8-bit boundary for Delphi compatibility, but it does'nt work properly. Do I make mistakes here? You can test this with the following two little programs:

-------------------------------------------------------
(* FPC Library *)
library lib; {lib.dll}
{$PACKRECORDS 8}
type TVector = record
                x,y,z: extended;
               end;
     TExternalData = record
                      p: array[1..2] of TVector;
                     end;
{$PACKRECORDS DEFAULT}
procedure DLLProc(var ED:TExternalData); stdcall;
var i: longint;
begin
 for i:=1 to 2 do
 begin
  ED.p[i].x:=1e-1; ED.p[i].y:=1e-2; ED.p[i].z:=1e-3;
 end;
end;
exports DLLProc name 'DLLProc';
end.
-------------------------------------------------------
(* Delphi Program *)
program caller;
{$APPTYPE CONSOLE}
type TVector = record
                x,y,z: extended;
               end;
     TExternalData = record
                      p: array[1..2] of TVector;
                     end;
var ED: TExternalData;
    i : longint;
procedure DLLProc(var ED: TExternalData); stdcall; external 'lib.dll';
begin
 DLLProc(ED);
 for i:=1 to 2 do writeln(ED.p[i].x,' ',ED.p[i].y,' ',ED.p[i].z);
 readln;
end.
-------------------------------------------------------

Thank you in advance,

Stefan

Reply by email

Marco van de Voort
to Gheorghe Stefan BERINDE
Show quoted text 8 lines
Hello all, I encounter some difficulties when passing a record variable from a FPC dynamic link library to a Delphi host program. I try to align the record structure on 8-bit boundary for Delphi compatibility, but it does'nt work properly. Do I make mistakes here? You can test this with the following two little programs:

packrecords takes the argument in bytes, not bits.

Reply by email

Gheorghe Stefan BERINDE
Show quoted text 11 lines
> > Hello all, > > I encounter some difficulties when passing a record variable from > a FPC dynamic link library to a Delphi host program. > I try to align the record structure on 8-bit boundary for Delphi > compatibility, but it does'nt work properly. Do I make mistakes here? > You can test this with the following two little programs: packrecords takes the argument in bytes, not bits.

Ok, it was my typo error! I mean 8-bytes boundary alignment, of course.
So, for the structure

	{$PACKRECORDS 8}
	type TPoint = record
        	       x,y: extended;
	              end;

the function SizeOf(TPoint) reports 32 bytes in FPC, since in Delphi it reports 24 bytes (with the same 8-bytes alignment). It seems for me that we have an incompatible records alignment between these compilers.
Am I correct here?

Stefan

Reply by email

Peter Vreman
Show quoted text 27 lines
> > > > Hello all, > > > > I encounter some difficulties when passing a record variable from > > a FPC dynamic link library to a Delphi host program. > > I try to align the record structure on 8-bit boundary for Delphi > > compatibility, but it does'nt work properly. Do I make mistakes here? > > You can test this with the following two little programs: > > > packrecords takes the argument in bytes, not bits. Ok, it was my typo error! I mean 8-bytes boundary alignment, of course. So, for the structure {$PACKRECORDS 8} type TPoint = record x,y: extended; end; the function SizeOf(TPoint) reports 32 bytes in FPC, since in Delphi it reports 24 bytes (with the same 8-bytes alignment). It seems for me that we have an incompatible records alignment between these compilers. Am I correct here?

extended is 12 bytes, the next 8 byte boundary is 16 bytes. Having that twice gives 32 bytes. So FPC does it correct. But Delphi calculates the alignment different and doesn't correspond with the 8 byte boundary as you asked for.

Reply by email

Michael Van Canneyt
to Peter Vreman
Peter Vreman wrote 31 lines
> > > > > > > Hello all, > > > > > > I encounter some difficulties when passing a record variable from > > > a FPC dynamic link library to a Delphi host program. > > > I try to align the record structure on 8-bit boundary for Delphi > > > compatibility, but it does'nt work properly. Do I make mistakes here? > > > You can test this with the following two little programs: > > > > > > packrecords takes the argument in bytes, not bits. > > Ok, it was my typo error! I mean 8-bytes boundary alignment, > of course. > So, for the structure > > {$PACKRECORDS 8} > type TPoint = record > x,y: extended; > end; > > the function SizeOf(TPoint) reports 32 bytes in FPC, since in > Delphi it reports 24 bytes (with the same 8-bytes alignment). > It seems for me that we have an incompatible records alignment > between these compilers. > Am I correct here? extended is 12 bytes, the next 8 byte boundary is 16 bytes. Having that twice gives 32 bytes. So FPC does it correct. But Delphi calculates the alignment different and doesn't correspond with the 8 byte boundary as you asked for.

Normally, doing a {$PACKRECORDS 4} or even 1 would give the same result as Delphi. The alignment in delphi is normally 2 bytes.

Michael.

Reply by email