Hello

Is it possible to declare Swift structs that have C compatible memory layout?

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?


// Swift

struct Foo {
    var x: Int32;
    var a: Int8;
}

struct Bar {
    var foo: Foo;
    var b: Int8;
}

print(sizeof(Foo)); // output: 5
print(sizeof(Bar)); // output: 6


// C

typedef struct {
    int x;
    char a;
} Foo;

typedef struct {
    Foo foo;
    char b;
} Bar;

int main() {
    printf("%lu\n", sizeof(Foo)); // output: 8
    printf("%lu\n", sizeof(Bar)); // output: 12
    return 0;
}


Best
KS Sreeram

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to