MemoryLayout in Swift

Photo by Fredy Jacob on Unsplash

Prerequisites:

Terminology

Memory Layout

Size, stride and alignment for Booleans

MemoryLayout<Bool>.size // 1
MemoryLayout<Bool>.stride // 1
MemoryLayout<Bool>.alignment // 1

The minimal example

Calculating Stride

The Case with a Size of Zero

MemoryLayout<())>.size // 0
MemoryLayout<()>.stride // 1
MemoryLayout<()>.alignment // 1

Calculating Alignment

The theory

An Example with different size and stride values

An Example with different size and stride values

Classes

An example with a Class

The size of a class object

The actual size of a class

Conclusion

Extend your knowledge

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store