gpt4 book ai didi

ios - 如何获取 UnsafeMutableRawPointer 的值?

转载 作者:可可西里 更新时间:2023-11-01 00:36:05 26 4
gpt4 key购买 nike

我正在尝试获取 UnsafeMutableRawPointer 指向的地址,但我无法这样做。我也是 Swift 的新手,所以我可能会遗漏某些东西或做错了。我最好将原始值转换为 CChar。

enter image description here

最佳答案

路人注意:我的大部分回答都没有意义,因为它没有回答上面最初的问题,而是回答了 chat 中出现的问题。与 OP。

花了我几个小时,但现在我已经学会了一些汇编,我可以回答一些问题了。

  1. CChar 是一个 C Char ...字面意思。它表示 Cchar 类型。它是 Int8typealias。它是一个字节。你不能像指针类型一样使用它,因为它们是 8 个字节(在 64 位机器上)。

  2. 您不需要所有这些 UnsafeMutableRawPointer 样板,而且您当然不需要访问它的原始值。您可以在需要指针的地方直接传递数组。

    When a function is declared as taking an UnsafePointer argument, it can accept any of the following: ...

    • A [Type] value, which is passed as a pointer to the start of the array.

    来自 Interacting with C APIs - pointers .

  3. 您遇到的问题是您的 0x8(%rdi) 似乎没有反射(reflect)在 Swift 端。这里的问题是您正在以 8 个字节的偏移量写入,但是 print(a.load(as: void_star.self)) 正在读取第一个字节。您正在读取一个您从未修改过的字节。

我做了一些进一步的探索。这是我冒险的战利品:

exampleSwift.swift:

@_silgen_name("incrementByValue")
func incrementByValue(_: Int64)

@_silgen_name("incrementByReference")
func incrementByReference(_: inout Int64)

@_silgen_name("return1234")
func return1234() -> Int64

@_silgen_name("incrementElements")
func incrementElements(of _: UnsafeRawPointer, count _: Int)

var a: Int64 = 100
print("\"a\" before \"incrementByValue(a)\": \(a)")
incrementByValue(a)
print("\"a\" after \"incrementByValue(a)\": \(a)\n")

var b: Int64 = 200
print("\"b\" before \"incrementByValue(b)\": \(b)")
incrementByReference(&b)
print("\"b\" after \"incrementByValue(b)\": \(b)\n")

print("return1234() returned: \(return1234())\n")

var array: [Int64] = Array(0...5)

print("\"array\" before incrementElements(of: array, count: array.count): \n\t\(array)")
incrementElements(of: array, count: array.count)
print("\"array\" after incrementElements(of: array, count: array.count): \n\t\(array)\n")

exampleASM.s:

.text

.globl _incrementByValue
.globl _incrementByReference
.globl _return1234
.globl _incrementElements

// A test routine that demonstrates operating on a value
_incrementByValue:
// %rdi contains a copy of the argument passed in.
// Changes here won't be reflected back in Swift
incq %rdi
ret

// A test routine that demonstrates operating on a reference
_incrementByReference:
// %rdi contains a reference tp the argument passed in.
// Changes to the reference itself won't be reflected back in Swift,
// but changes to the referenced memory will.
incq (%rdi)
ret

// A test routine that demonstrates the use of %rax for returning a value
_return1234:
movq $1234, %rax // return value is in rax
ret

//A test routine that demonstrates operating on an array
_incrementElements:
// %rdi: Pointer to first of n Int64 elements
// %rsi: the array count, n

movq %rsi, %rcx // Set loop counter (%rcx) to n
aLoop:
incq (%rdi) // increment value pointer to by %rdi
add $8, %rdi // advance pointer by 8 bytes
loop aLoop // loop back to aLoop if rcx > 0

ret

编译、链接和运行:

llvm-g++ -c exampleASM.s &&
swiftc -c exampleSwift.swift &&
ld exampleASM.o exampleSwift.o -o exampleBinary -force_load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_macosx.a -framework CoreFoundation -macosx_version_min 10.12.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -lobjc -lSystem -arch x86_64 -L /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -rpath /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -no_objc_category_merging &&
./exampleBinary

输出:

"a" before "incrementByValue(a)": 100
"a" after "incrementByValue(a)": 100

"b" before "incrementByValue(b)": 200
"b" after "incrementByValue(b)": 201

return1234() returned: 1234

"array" before incrementElements(of: array, count: array.count):
[0, 1, 2, 3, 4, 5]
"array" after incrementElements(of: array, count: array.count):
[1, 2, 3, 4, 5, 6]

关于ios - 如何获取 UnsafeMutableRawPointer 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40569800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com