[Issue 19516] New: Alignment of members & size of structs inconsistent with C
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Dec 26 15:36:46 UTC 2018
https://issues.dlang.org/show_bug.cgi?id=19516
Issue ID: 19516
Summary: Alignment of members & size of structs inconsistent
with C
Product: D
Version: D2
Hardware: x86_64
OS: All
Status: NEW
Severity: normal
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: bdh_bugzilla at outlook.com
According to the spec [1], D structs are supposed to have the same memory
layout as their C counterparts. However, writing some bindings for a C library
I found the following inconsistency.
-- D code (see [2])
struct s {
union {
double d;
char[20] cs;
};
char b;
}
pragma(msg, s.b.offsetof); // Expecting 24 but getting 20
pragma(msg, s.sizeof); // Expecting 32 but getting 24
void main(){}
--
In C (using clang 7 or gcc 8.1 [3]) the offset of b is 20, but 24 when using
dmd/ldc [2]. The mismatching sizeof output seems to be a result of that.
-- C code (see [3])
#include <stdio.h>
struct S {
union {
double d;
char cs[20];
};
char b;
};
int main(){
struct S s;
// Yields 24, unlike the 20 in D
printf("b at: %lu\n", (void*)&s.b-(void*)&s);
// Yields size 32, unlike the 24 in D
printf("size of s: %lu\n", sizeof(s));
}
--
[1] https://dlang.org/spec/struct.html#struct_layout
[2] https://dpaste.dzfl.pl/ff55c816a940
[3] https://www.jdoodle.com/a/Sgc
--
More information about the Digitalmars-d-bugs
mailing list