Deep copy, shallow copy, and COW
In Swift you assign a large array to another variable with var b = a and then modify only b. Is what happens a shallow copy or a deep copy? Explain it together with Copy-on-Write.
In Swift you assign a large array to another variable with var b = a and then modify only b. Is what happens a shallow copy or a deep copy? Explain it together with Copy-on-Write.
At assignment only a shallow copy happens: both variables point at the same heap buffer. The first time b is modified, Swift checks whether the buffer is uniquely referenced and only then copies it in full before writing; that's Copy-on-Write (COW), and if the elements are classes the references in the copied buffer still point at the original instances, so it isn't a complete copy.
Memory and ARCShort answerAdvanced
… After s2.a = 9, are the addresses where s1.a and s2.a are stored the same or different…
#copy #cow
SwiftShort answerExpert
You need a struct ImageBuffer that wraps a large pixel buffer, keeps value semantics, and…
#cow #value-semantics
Memory and ARCMultiple choiceBasics
In Objective-C, why do you use copy instead of strong for an NSString property, as in…
#copy
Memory and ARCShort answerAdvanced
How far is it true that "using a struct means no ARC cost"?
#cow