<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hello</div><div class=""><br class=""></div><div class="">Is it possible to declare Swift structs that have C compatible memory layout?</div><div class=""><br class=""></div><div class="">In the code below, Foo and Bar have different sizes in Swift and C. Is it possible to make the Swift structs have the same layout as their C counterparts?</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><font face="Courier New" class="">// Swift</font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">struct Foo {</font></div><div class=""><font face="Courier New" class=""> var x: Int32;</font></div><div class=""><font face="Courier New" class=""> var a: Int8;</font></div><div class=""><font face="Courier New" class="">}</font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">struct Bar {</font></div><div class=""><font face="Courier New" class=""> var foo: Foo;</font></div><div class=""><font face="Courier New" class=""> var b: Int8;</font></div><div class=""><font face="Courier New" class="">}</font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">print(sizeof(Foo)); // output: 5</font></div><div class=""><font face="Courier New" class="">print(sizeof(Bar)); // output: 6</font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">// C</font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">typedef struct {</font></div><div class=""><font face="Courier New" class=""> int x;</font></div><div class=""><font face="Courier New" class=""> char a;</font></div><div class=""><font face="Courier New" class="">} Foo;</font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">typedef struct {</font></div><div class=""><font face="Courier New" class=""> Foo foo;</font></div><div class=""><font face="Courier New" class=""> char b;</font></div><div class=""><font face="Courier New" class="">} Bar;</font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">int main() {</font></div><div class=""><font face="Courier New" class=""> printf("%lu\n", sizeof(Foo)); // output: 8</font></div><div class=""><font face="Courier New" class=""> printf("%lu\n", sizeof(Bar)); // output: 12</font></div><div class=""><font face="Courier New" class=""> return 0;</font></div><div class=""><font face="Courier New" class="">}</font></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Best</div><div class="">
<div class="">KS Sreeram</div><div class=""></div><div class=""><br class=""></div></div></body></html>