What is MemoryLayout? size, stride, alignment
Essence
MemoryLayout<T> is the compiler's static "ruler" for type T. Three numbers:
- size — how many bytes the value itself occupies (without trailing padding);
- alignment — the address boundary the value must sit on (address divisible by alignment); for a composite type it equals the largest alignment of its fields;
- stride — the distance between neighboring elements in an array:
sizerounded up to a multiple ofalignment.strideis what the pointer advances by when you walk anUnsafeBufferPointer.
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep