How can I get the memory address of a value type or a custom struct in Swift?

后端 未结 3 627
一整个雨季
一整个雨季 2021-02-04 08:08

I\'m trying to gain a deeper understanding of how Swift copies value types:

The behavior you see in your code will always be as if a copy took place. Ho

3条回答
  •  醉话见心
    2021-02-04 08:44

    According to Martin R' s answer

    addressOf() cannot be used with struct variables. String is a struct, however, it is automatically bridged to NSString when passed to a function expecting an object.

    According to nschum's answer, you can get the (stack) address of a struct, build-in type or object reference like this:

    import UIKit
    
    
    func address(o: UnsafePointer) -> Int {
        return unsafeBitCast(o, Int.self)
    }
    
    func addressHeap(o: T) -> Int {
        return unsafeBitCast(o, Int.self)
    }
    
    
    struct myStruct {
        var a: Int
    }
    
    class myClas {
    
    }
    //struct
    var struct1 = myStruct(a: 5)
    var struct2 = struct1
    print(NSString(format: "%p", address(&struct1))) // -> "0x10f1fd430\n"
    print(NSString(format: "%p", address(&struct2))) // -> "0x10f1fd438\n"
    
    //String
    var s = "A String"
    var aa = s
    print(NSString(format: "%p", address(&s))) // -> "0x10f43a430\n"
    print(NSString(format: "%p", address(&aa))) // -> "0x10f43a448\n"
    
    //Class
    var class1 = myClas()
    var class2 = class1
    print(NSString(format: "%p", addressHeap(class1))) // -> 0x7fd5c8700970
    print(NSString(format: "%p", addressHeap(class2))) // -> 0x7fd5c8700970
    
    unsafeAddressOf(class1) //"UnsafePointer(0x7FD95AE272E0)"
    unsafeAddressOf(class2) //"UnsafePointer(0x7FD95AE272E0)"
    
    //Int
    var num1 = 55
    var num2 = num1
    print(NSString(format: "%p", address(&num1))) // -> "0x10f1fd480\n"
    print(NSString(format: "%p", address(&num2))) // -> "0x10f1fd488\n"
    

    One thing I found is, if myStruct has no value, the address will be retain same:

    struct myStruct {
    
    }
    
    var struct1 = myStruct()
    var struct2 = struct1
    print(NSString(format: "%p", address(&struct1))) // -> ""0xa000000000070252\n""
    print(NSString(format: "%p", address(&struct2))) // -> ""0xa000000000070252\n""
    

提交回复
热议问题