Struct size

Lars T. Kyllingstad via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 19 05:08:03 PDT 2014


Say I have two structs, defined like this:

     struct A { /* could contain whatever */ }

     struct B { A a; }

My question is, is it now guaranteed that A.sizeof==B.sizeof, 
regardless of how A is defined (member variable types, alignment, 
etc.)?  More to the point, say I have a function foo() which 
looks like this:

     extern(C) void foo(A* ptr, size_t len);

Is it now guaranteed that I can safely pass it a pointer to an 
array of Bs?  That is,

     auto arr = new B[10];
     foo(cast(A*) arr.ptr, arr.length);

Thanks,
Lars


More information about the Digitalmars-d-learn mailing list