How does MemoryLayout calculate size for a struct ?
I read this article, and I seemed to understand it pretty well, until I got stuck on the following problem:
struct CertifiedPuppy1 {
let age: Int
let isTrained: Bool
let isCertified: Bool
}
MemoryLayout<CertifiedPuppy1>.size // 10
MemoryLayout<CertifiedPuppy1>.stride // 16
MemoryLayout<CertifiedPuppy1>.alignment // 8
struct CertifiedPuppy2 {
let isTrained: Bool
let age: Int
let isCertified: Bool
}
MemoryLayout<CertifiedPuppy2>.size // 17
MemoryLayout<CertifiedPuppy2>.stride // 24
MemoryLayout<CertifiedPuppy2>.alignment // 8
struct CertifiedPuppy3 {
let isTrained: Bool
let isCertified: Bool
let age: Int
}
MemoryLayout<CertifiedPuppy3>.size // 16 <--why not 10, like for CertifiedPuppy1 ??--
MemoryLayout<CertifiedPuppy3>.stride // 16
MemoryLayout<CertifiedPuppy3>.alignment // 8
Question is, why does CertifiedPuppy3
have size 16, instead of 10, like for CertifiedPuppy1
?
Int
is aligned to a 8-byte boundary and has size 8. – Alcheringa