> Delphi structures are incompatible to c structures.
> Especially when you use "packed structures" in c.
>
> I tried a lot of stuff with it, and it was purely chaos.
Delphi has four ways of aligning {$a1},{$a2},{$a4} and {$a8}
If I understood the delphi help correctly, this means a _fixed_ alignment,
iow
{$a1} all fields aligned on 1 byte (equal to declaring record packed)
{$a2} all fields aligned on 2 byte
etc.
C alignment (at least e.g. gcc, don't know the Windows compilers that well)
is a bit different. It typically aligns each element on the size of the
element, so alignment is not constant throughout the record:
Assume we would have C alignment, then:
type myrec = record
a : byte; // size 1 aligned on 1. Is always. offset 0
b : word; // size 2 must be aligned on 2. offset 2, one byte skipped.
c : dword; // size 4 must be aligned on 4. offset 4, none skipped
a2: byte; // size 1 aligned on 1. offset 8,
e: int64; // size 8 must be aligned on 8. offset 16, 7 skipped
end;
The above records has 1+2+4+1+8=16 bytes worth of fields.
directive size
packed or $a1 16
$a2 18 (easy to see, the bytes need to be aligned)
$a4 24 (byte + word fields need to be alligned to 4 bytes. 2x3+1x2)
$a8 30
<(gc)c alignment> 24 (see above comments in record def)
One can see that the C alignment doesn't match with any of the $a ones. The only with the correct size
is $a4, and $a4 already aligns the second field differently.
However one can always lay this out by inserting dummy fields that take the place of alignment in a packed
record.
E.g. again the above record, that can't be represented by Delphi (at least afaik):
type myrec = packed record
a : byte;
dummy1: byte;
b : word;
c : dword;
a2: byte;
dummy2:array[0..6] of byte; // 7 byttes;
e: int64;
end;
The free unices (FreeBSD, linux) quite often manually align their structs this way, to be more compiler
independant in their type declarations.
------------------------ Yahoo! Groups Sponsor --------------------~-->
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/i7folB/TM
--------------------------------------------------------------------~->
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Delphi-JEDI/
<*> To unsubscribe from this group, send an email to:
[email protected]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.