ldc-developers/ldc — Issue #28: 64-bit unions are not the same size as C language unions in some situations
Author: bll123
Created: 2011-11-18 18:42:14+00:00
State: open

Works on 32bit.

** C:

```C
#include
#include

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

bll-desktop:bll$ cc -o x1c x1.c
bll-desktop:bll$ ./x1c
C
sizeof a: 32
sizeof a.a1: 16
sizeof a.ac: 20
offsetof a.ac: 0

** D2:

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

bll-desktop:bll$ ldc2 -d x1.d
bll-desktop:bll$ ./x1
D
sizeof a: 24
sizeof a.a1: 16
sizeof a.ac: 20
offsetof a.ac: 0

bll-desktop:bll$ ldc2 --version
LLVM D Compiler LDC trunk
based on DMD v2.052 and LLVM 2.9

** D1:

```D
alias char[] string;
import io.Stdout;

union ua {
real a1;
char ac[20];
};

int
main (string argv[]) {
ua a;
Stdout ("D1\n");
Stdout ("sizeof a: ", a.sizeof, "\n");
Stdout ("sizeof a.a1: ", a.a1.sizeof, "\n");
Stdout ("sizeof a.ac: ", a.ac.sizeof, "\n");
Stdout ("offsetof a.ac: ", a.ac.offsetof, "\n");
return 0;
}
```

bll-desktop:bll$ ldc x2.d
bll-desktop:bll$ ./x2
D1
sizeof a: , 24,
sizeof a.a1: , 16,
sizeof a.ac: , 20,
offsetof a.ac: , 0,

bll-desktop:bll$ ldc --version
LLVM D Compiler LDC trunk
based on DMD v1.067 and LLVM 2.9

I used (9/18/2011), though this bug is still extant:

hg clone 'https://bitbucket.org/lindquist/ldc'

git clone 'https://github.com/AlexeyProkhin/phobos'

git clone 'https://github.com/AlexeyProkhin/druntime'

svn co 'http://svn.dsource.org/projects/tango/tags/releases/0.99.9' tango

patch -p0 -d tango/ < tango-0.99.9.patch