Why are ‘Set’ and ‘Dictionary’ memory addresses different when deep copying is done?

I recently learned about CoW (copy-on-write). However, I have a question.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>func address(of object: UnsafeRawPointer) -> String {
let address: Int = Int(bitPattern: object)
return String(format: "%p", address)
}
var arr: Array<Int> = [1, 2, 3]
var newArr: Array<Int> = arr
print("Addr of `arr`: (address(of: &arr))") // 0x600002e601e0
print("Addr of `newArr`: (address(of: &newArr))") // 0x600002e601e0
</code>
<code>func address(of object: UnsafeRawPointer) -> String { let address: Int = Int(bitPattern: object) return String(format: "%p", address) } var arr: Array<Int> = [1, 2, 3] var newArr: Array<Int> = arr print("Addr of `arr`: (address(of: &arr))") // 0x600002e601e0 print("Addr of `newArr`: (address(of: &newArr))") // 0x600002e601e0 </code>
func address(of object: UnsafeRawPointer) -> String {
    
    let address: Int = Int(bitPattern: object)
    return String(format: "%p", address)
}

var arr: Array<Int> = [1, 2, 3]
var newArr: Array<Int> = arr

print("Addr of `arr`: (address(of: &arr))")        // 0x600002e601e0
print("Addr of `newArr`: (address(of: &newArr))")  // 0x600002e601e0

Both instances have the same memory address before changing the data in the array. However…

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var setValue: Set<Int> = [1, 3, 5]
var newSetValue: Set<Int> = setValue
var dict: Dictionary<String, Int> = ["zero": 0, "one": 1]
var newDict: Dictionary<String, Int> = dict
print("Addr of `setValue`: (address(of: &setValue))") // 0x10000c098
print("Addr of `newSetValue`: (address(of: &newSetValue))") // 0x10000c0a0
print()
print("Addr of `dict`: (address(of: &dict))") // 0x10000c0a8
print("Addr of `newDict`: (address(of: &newDict))") // 0x10000c0b0
</code>
<code>var setValue: Set<Int> = [1, 3, 5] var newSetValue: Set<Int> = setValue var dict: Dictionary<String, Int> = ["zero": 0, "one": 1] var newDict: Dictionary<String, Int> = dict print("Addr of `setValue`: (address(of: &setValue))") // 0x10000c098 print("Addr of `newSetValue`: (address(of: &newSetValue))") // 0x10000c0a0 print() print("Addr of `dict`: (address(of: &dict))") // 0x10000c0a8 print("Addr of `newDict`: (address(of: &newDict))") // 0x10000c0b0 </code>
var setValue: Set<Int> = [1, 3, 5]
var newSetValue: Set<Int> = setValue

var dict: Dictionary<String, Int> = ["zero": 0, "one": 1]
var newDict: Dictionary<String, Int> = dict

print("Addr of `setValue`: (address(of: &setValue))")        // 0x10000c098
print("Addr of `newSetValue`: (address(of: &newSetValue))")  // 0x10000c0a0
print()

print("Addr of `dict`: (address(of: &dict))")        // 0x10000c0a8 
print("Addr of `newDict`: (address(of: &newDict))")  // 0x10000c0b0

As far as I know, ‘CoW (copy-on-write)’ copies data when the value changes and shares the original resources before the value changes. In my opinion, ‘Array’ seems to share the same resources before the value changes. However, I wonder why ‘Set’ and ‘Dictionary’ have different memory address values when the value changes have not occurred.

(p.s. I understand that CoW is implemented internally in the ‘Collection Type’.)

New contributor

홍진표 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

You should see some warnings like this if you are using Swift 5.10:

Forming ‘UnsafeRawPointer’ to a variable of type ‘Set’; this is likely incorrect because ‘Set’ may contain an object reference.

This warning is added to point out this exact mistake that you are making.

Converting an Array to UnsafeRawPointer using the & prefix gets you a pointer to the internal storage of the array, but converting Sets and Dictionarys do not have the same behaviour. You just get the stack address of that var. dict and newDict are different variables, so of course their addresses are different.

I don’t think there is any public APIs for getting the address of the internal storage of a Set or Dictionary. For other types like Data, there is Data.withUnsafeBytes, though.

If you want to demonstrate copy-on-write behaviour with Set and Dictionary, I would use the “Allocations” tool in Instruments.app. It will count how many instances of the dictionary/set storage are allocated. My answer here demonstrates this for Set.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật