Daily iOS
Memory and ARC

Struct copies and COW buffers

struct Example { var a: Int; var tags: [String] }
let s1 = Example(a: 1, tags: ["x"])
var s2 = s1
s2.a = 9

After s2.a = 9, are the addresses where s1.a and s2.a are stored the same or different, and what about the buffer address of the tags array?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

s1 and s2 are separate values, so a is at a different address from the start (inline copy). For tags, the Array struct is copied but the heap buffer is shared, so the address is the same until s2.tags is mutated and COW copies it.

Share a question