Bug 8383 – 64-bit ABI: unions not the same size as in C in some situations
Status
RESOLVED
Resolution
WORKSFORME
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2012-07-13T02:55:00Z
Last change time
2012-11-27T09:30:06Z
Assigned to
nobody
Creator
code
Comments
Comment #0 by code — 2012-07-13T02:55:33Z
Originally reported by "bll" at http://www.dsource.org/projects/ldc/ticket/475:
---
#include <stdio.h>
#include <stdlib.h>
union a {
long double a1;
char ac[20];
};
int
main (int argc, char *argv[]) {
union a a;
printf ("C\n");
printf ("sizeof a: %d\n", sizeof (a));
printf ("sizeof a.a1: %d\n", sizeof (a.a1));
printf ("sizeof a.ac: %d\n", sizeof (a.ac));
printf ("offsetof a.ac: %d\n", (int) ((char *) &a.ac - (char *) &a.a1));
return 0;
}
---
yields
---
C
sizeof a: 32
sizeof a.a1: 16
sizeof a.ac: 20
offsetof a.ac: 0
---
but
---
import std.stdio;
union ua {
real a1;
char ac[20];
};
int
main (string argv[]) {
ua a;
writefln ("D");
writefln ("sizeof a: %d", a.sizeof);
writefln ("sizeof a.a1: %d", a.a1.sizeof);
writefln ("sizeof a.ac: %d", a.ac.sizeof);
writefln ("offsetof a.ac: %d", a.ac.offsetof);
return 0;
}
---
yields
---
D
sizeof a: 24
sizeof a.a1: 16
sizeof a.ac: 20
offsetof a.ac: 0
---
when compiling on Linux x86_64.
Comment #1 by clugdbug — 2012-11-27T02:54:38Z
With github head for D2, this gives sizeof a: 32
when compiled with -m64 on Linuxx86_64.
It only gives sizeof: a == 20 when compiled with -m32.
This is true even on 1.073, released five months before this bug report was made.
So I cannot reproduce this at all. Did the original poster fail to use the -m64 flag?
It could of course have been an even older bug which was fixed before the start of 2012.
Comment #2 by code — 2012-11-27T09:30:06Z
Hm, strange, I was sure I could repro this using both DMD and LDC at some point (and yes, building with -m64). In any case, thanks for taking the time to look at this.