Bug 2821 – struct alignment inconsistent with C for { int, long }
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Linux
Creation time
2009-04-09T00:08:00Z
Last change time
2014-04-18T09:12:07Z
Keywords
wrong-code
Assigned to
bugzilla
Creator
kamm-removethis
Comments
Comment #0 by kamm-removethis — 2009-04-09T00:08:02Z
I couldn't find it in the spec right now, but as far as I remember D and C structs are supposed to be aligned identically when the same types are used.
struct S {
int i;
long l;
}
In C (using gcc 4.1.2), the S.l.offsetof is 4 and the total size is 12. With dmd the offsetof is 8 and the total size is 16.
Test code:
-- D code
import std.stdio;
struct S {
int i;
long l;
}
void main() {
writefln("%d %d", S.sizeof, S.l.offsetof);
}
-- C code
#include <stdio.h>
struct S
{
int i;
long long l;
};
int main()
{
struct S s;
printf("%d %d\n", sizeof(s), (void*)&s.l - (void*)&s);
return 1;
}
--
Comment #1 by smjg — 2009-04-09T05:12:53Z
Have you tried DMC and GDC to compare?
One would expect GDC to do the same as GCC, and DMD to do the same as DMC. But how does each decide what alignment to default to in the first place?
Comment #2 by tomas — 2009-04-09T05:33:32Z
DMD on Win32 should obviously match DMC, but DMD on other platforms should match the companion C compiler there, which is GCC.
GCC on x86-32 aligns int64 to 4 bytes (at least on Linux). There was a similar issue with double that was fixed in 1.042: http://d.puremagic.com/issues/show_bug.cgi?id=2692