Search code examples
arraysswiftunsafememory-layout

Size of arrays in MemoryLayout.size in Swift


Sorry if the question may be duplicate I neither could find it here nor using Google.

I'm new to unsafe Swift and I was wondering why the size of an array of Bools which is e.g 10 bytes, is still 8 bytes ?

If I wasn't able to say what I meant I would be wondering if you could take a look at this code :

var boolArray = [Bool]()
for _ in 1...10{
    boolArray.append(true)
}
print(MemoryLayout.size(ofValue: boolArray))

I don't get why it prints 8 while the array has 10 bools which at least contains of 10 bytes.


Solution

  • Because the result of function size does not include any dynamically allocated or out of line storage. See MemoryLayout.size(ofValue:)