Skip to content

Instantly share code, notes, and snippets.

@artem-shmatkov
Forked from LucianoPAlmeida/COW.swift
Created March 27, 2021 07:55
Show Gist options
  • Save artem-shmatkov/d56d19c30bf870724ebeaea1a5b9b5ee to your computer and use it in GitHub Desktop.
Save artem-shmatkov/d56d19c30bf870724ebeaea1a5b9b5ee to your computer and use it in GitHub Desktop.
Copy-on-Write
import Foundation
func print(address o: UnsafeRawPointer ) {
print(String(format: "%p", Int(bitPattern: o)))
}
var array1: [Int] = [0, 1, 2, 3]
var array2 = array1
//Print with just assign
print(address: array1) //0x600000078de0
print(address: array2) //0x600000078de0
//Let's mutate array2 to see what's
array2.append(4)
print(address: array2) //0x6000000aa100
//Output
//0x600000078de0 array1 address
//0x600000078de0 array2 address before mutation
//0x6000000aa100 array2 address after mutation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment