Swift查看变量内存地址

withUnsafePointer

不说话,先放代码

plain_text 复制代码
        withUnsafeBufferPointer(to: a) { point in
            let address = UnsafeRawPointer(point)
            let addressInt = Int(bitPattern: address)
            print("\(addressInt)")
        }

验证copy on write

值类型的赋值会对对象进行拷贝,对于一些容器,阿果做了为了避免Array、Set复制导致的性能损失, 会copy on write的机制进行优化。

对于 Array、Dictionary、Set 类型,当它们赋值的时候不会发生拷贝,只有在修改的之后才会发生拷贝。

当我们用代码验证的时候,在尝试使用withUnsafePointer的时候,会发现数组赋值之后,两个变量的地址会不一样,当然这也是合理,毕竟值类型赋值之后两个变量在内存上应该互为独立个体才对。

如果要验证COW,Array给我们提供了另一个方法,那就是withUnsafeBufferPointer,用此方法查看赋值后的数组,会看到输出的地址不变。

实际上withUnsafeBufferPointer指向的地址是数组存储数据的内存地址,withUnsafePointer指向的。

代码举例

plain_text 复制代码
        var array1 = [1, 2, 3]
        array1.withUnsafeBufferPointer { point in
            let address = point.baseAddress!
            let addressInt = Int(bitPattern: address)
            print("\(addressInt)")
            // 105553141216992
        }
        withUnsafePointer(to: array1) { point in
            let address = UnsafeRawPointer(point)
            let addressInt = Int(bitPattern: address)
            print("\(addressInt)")
            // 6089438336
        }
        var array2 = array1  // 这里并没有真正复制数据,而是共享内存
        array2.withUnsafeBufferPointer { point in
            let address = point.baseAddress!
            let addressInt = Int(bitPattern: address)
            print("\(addressInt)")
            // 105553141216992
        }
        withUnsafePointer(to: array2) { point in
            let address = UnsafeRawPointer(point)
            let addressInt = Int(bitPattern: address)
            print("\(addressInt)")
            // 6089438320
        }
        array2[2] = 1
        array2.withUnsafeBufferPointer { point in
            let address = point.baseAddress!
            let addressInt = Int(bitPattern: address)
            print("\(addressInt)")
            // 105553141218080
        }
        withUnsafePointer(to: array2) { point in
            let address = UnsafeRawPointer(point)
            let addressInt = Int(bitPattern: address)
            print("\(addressInt)")
            // 6089438280
        }