Today I had a problem with Swift structures. See the following structure, for example:
struct TestStruct { var test: UInt16 var test2: UInt32 }
I would suggest that the size of this structure is 6 bytes in total, because UInt16 is 2 bytes and UInt32 is 4 bytes.
The following code prints 6 (this is correct):
println(sizeof(UInt32) + sizeof(UInt16))
But if I check the size of the structure with
println(sizeof(TestStruct))
he prints 8, not 6.
If I try to do the same with the following structure:
struct TestStruct2 { var test: UInt16 var test2: UInt16 var test3: UInt16 }
Then
println(sizeof(TestStruct2))
displays the correct value: 6. But the size of TestStruct should be the same as the size of TestStruct2.
Why is the first structure 8 bytes in size? Am I doing something wrong or is this a mistake?
(tested with Xcode 6.1.1 (6A2008a) on OS X Mavericks, the usual command line application for Mac)
struct sizeof xcode swift
user4650218
source share