I've been looking at the addresses of Swift array elements and I'm surprised by the results. Here's my code for Bool elements:
let boolArray = [false, true, false, true]
withUnsafePointer(to: boolArray[0]) { print($0) }
withUnsafePointer(to: boolArray[1]) { print($0) }
Here are the printed results:
0x000000016fdff40e
0x000000016fdff40c
It looks like the first element is stored two bytes after the second. Bizarre.
Here's my code for Float elements:
let floatArray: [Float] = [1.1, 2.2, 3.3, 4.4]
withUnsafePointer(to: floatArray[0]) { print($0) }
withUnsafePointer(to: floatArray[1]) { print($0) }
Here are the printed results:
0x000000016fdff408
0x000000016fdff400
Now the first value is stored eight bytes after the second. Just to check, here's another way to access memory locations:
withUnsafePointer(to: numArray) {
print($0)
print($0.advanced(by: 1))
}
Here are the printed results:
0x000000016fdff418
0x000000016fdff420
This time, the first element is before the second, but they're separated by eight bytes. The array contains Floats, so I'd expected the second address to be four bytes after the first.
I'm sure I'm missing something obvious. Any ideas?