Thread overview
[Issue 2821] New: struct alignment inconsistent with C for { int, long }
Apr 09, 2009
d-bugmail
Apr 09, 2009
d-bugmail
Apr 09, 2009
d-bugmail
May 15, 2009
Walter Bright
April 09, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=2821

           Summary: struct alignment inconsistent with C for { int, long }
           Product: D
           Version: 1.043
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: kamm-removethis@incasoftware.de


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;
}
--


-- 

April 09, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=2821


smjg@iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg@iname.com




------- Comment #1 from smjg@iname.com  2009-04-09 05:12 -------
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?


-- 

April 09, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=2821





------- Comment #2 from tomas@famolsen.dk  2009-04-09 05:33 -------
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


-- 

May 15, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=2821


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED




--- Comment #3 from Walter Bright <bugzilla@digitalmars.com>  2009-05-15 12:15:29 PDT ---
Fixed dmd 1.045 and 2.030

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------