如何获取 Swift 中值类型或自定义结构的内存地址?
How can I get the memory address of a value type or a custom struct in Swift?
我正在努力更深入地了解 Swift copies value types:
The behavior you see in your code will always be as if a copy took
place. However, Swift only performs an actual copy behind the scenes
when it is absolutely necessary to do so.
为了加深理解,我想获取值类型的内存地址。我尝试了 unsafeAddressOf(),但是 (例如,String 被强制转换为 NSString)。
如何获取值类型的内存地址,例如 Int 的实例或 Swift 中的自定义结构?
我不确定是否有 "recommended" 方法可以做到这一点,但一种方法是使用 withUnsafePointer(_:_:)
,像这样:
var s: String = "foo"
withUnsafePointer(&s) { NSLog("\([=10=])") }
这在我的机器上打印了 0x00007ffff52a011c8
。
根据 Martin R 的回答
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.
根据 nschum 的回答,您可以获得 struct、内置类型或对象引用的(堆栈)地址,如下所示:
import UIKit
func address(o: UnsafePointer<Void>) -> Int {
return unsafeBitCast(o, Int.self)
}
func addressHeap<T: AnyObject>(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"
我发现的一件事是,如果 myStruct
没有值,地址将保持不变:
struct myStruct {
}
var struct1 = myStruct()
var struct2 = struct1
print(NSString(format: "%p", address(&struct1))) // -> ""0xa000000000070252\n""
print(NSString(format: "%p", address(&struct2))) // -> ""0xa000000000070252\n""
Swift 2.0 :
你可以使用这个unsafeAddressOf(someObject)
或 Swift 3.0:
使用withUnsafePointer(to: someObejct) { print("\([=11=])") }
我正在努力更深入地了解 Swift copies value types:
The behavior you see in your code will always be as if a copy took place. However, Swift only performs an actual copy behind the scenes when it is absolutely necessary to do so.
为了加深理解,我想获取值类型的内存地址。我尝试了 unsafeAddressOf(),但是
如何获取值类型的内存地址,例如 Int 的实例或 Swift 中的自定义结构?
我不确定是否有 "recommended" 方法可以做到这一点,但一种方法是使用 withUnsafePointer(_:_:)
,像这样:
var s: String = "foo"
withUnsafePointer(&s) { NSLog("\([=10=])") }
这在我的机器上打印了 0x00007ffff52a011c8
。
根据 Martin R 的回答
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.
根据 nschum 的回答,您可以获得 struct、内置类型或对象引用的(堆栈)地址,如下所示:
import UIKit
func address(o: UnsafePointer<Void>) -> Int {
return unsafeBitCast(o, Int.self)
}
func addressHeap<T: AnyObject>(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"
我发现的一件事是,如果 myStruct
没有值,地址将保持不变:
struct myStruct {
}
var struct1 = myStruct()
var struct2 = struct1
print(NSString(format: "%p", address(&struct1))) // -> ""0xa000000000070252\n""
print(NSString(format: "%p", address(&struct2))) // -> ""0xa000000000070252\n""
Swift 2.0 :
你可以使用这个unsafeAddressOf(someObject)
或 Swift 3.0:
使用withUnsafePointer(to: someObejct) { print("\([=11=])") }