我有一个来自hdf5文件的浮点值数组(floatArray
),我想将此数据用作接受不安全[可变]指针(inputForFunction
)的函数的输入。我对不安全指针还不熟悉,所以我需要别人的帮助。
我想到的一个方法是,将数组存储到一个文件中,然后打开它,并将文件描述符映射到用于函数输入的变量的内存中:
// read the data from HDF5 file
var floatArray: [Float] = try originalHDF5DataSet.read()
// store the data
let dataToStore = NSData(bytes: &floatArray, length: sizeOfArray * MemoryLayout<Float>.size)
let tmpPath = URL(fileURLWithPath: NSTemporaryDirectory())
let filePath = tmpPath.appendingPathComponent("floatArrayData").appendingPathExtension("dat")
do{
try data.write(to: filePath, options: .atomic)
let fileManager : FileManager = FileManager.default
if fileManager.fileExists(atPath:filePath.path){
print("Success")
}else{
print("Fail")
}
// open the data
let dataPath = filePath.path
let fd = open(dataPath, O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
assert(fd != -1, "Error: failed to open output file at \""+dataPath+"\" errno = \(errno)\n")
// memory map the parameters
let hdr = mmap(nil, Int(sizeOfArray), PROT_READ, MAP_FILE | MAP_SHARED, fd, 0)
// cast Void pointers to Float
let inputForFunction = UnsafePointer<Float>(hdr!.assumingMemoryBound(to: Float.self))
} catch{
print("failed")
}
但是,它可能不是最好的实现,因为存储数据的过程似乎很耗时。
这就是我考虑使用unsafepointer将指针从
floatArray
传递到inputForFunction
的原因,但我不确定如何实现它。仅供参考,我想的一种方法是直接使用withUnsafePointer
方法:var floatArray: [Float] = try originalHDF5DataSet.read()
var inputForFunction = UnsafeMutablePointer<Float>.allocate(capacity: Int(sizeOfArray))
withUnsafePointer(to: &floatArray[0]) { (ptr: UnsafePointer<Float>) in
inputForFunction = UnsafeMutablePointer(mutating: ptr)
}
或者另一种方法是使用
withMemoryRebound
(但以下方法不起作用):var floatArray: [Float] = try originalHDF5DataSet.read()
var inputForFunction = UnsafeMutablePointer<Float>.allocate(capacity: Int(sizeOfArray))
withUnsafePointer(to: &floatArray) { (ptrDataArray: UnsafePointer<[Float]>) in
inputForFunction = ptrDataArray.withMemoryRebound(to: inputForFunction.self, capacity: Int(sizeOfArray), { (ptr: UnsafePointer<[Float]>) -> UnsafeMutablePointer<Float> in
return ptr[0]
})
}
我的问题是
如何将
floatArray
数据的地址传递给inputForFunction
?如果我需要从unsafe pointer转换为不安全的[mutable]指针,怎么做?
如果需要,如何使用
withMemoryRebound
实现此目的?我能指定闭包内
inputForFunction
的地址/值?当我检查了
floatArray
和floatArray[0]
的地址时,它们是不同的(这似乎不同于C/C++)。哪个地址应该传递给inputForFunction
? 最佳答案
您可以找到许多文章来解释如何从Unsafe(Mutable)Pointer<Float>
获取[Float]
。他们中的一些人回答了你的一些问题。以后再找吧。
如何将floatarray的数据地址传递给inputforfunction?
通常使用数组的方法withUnsafeBufferPointer
或withUnsafeMutableBufferPointer
。您可以在闭包参数中得到一个Unsafe(Mutable)BufferPointer<Element>
,它包含一个Unsafe(Mutable)Pointer<Element>
作为其baseAddress
属性。
var floatArray: [Float] = try originalHDF5DataSet.read()
floatArray.withUnsafeBufferPointer {unsafeBufferPointer in
let inputForFunction = unsafeBufferPointer.baseAddress
//`unsafeBufferPointer` (including its `baseAddress`) is valid only in this closure.
//So, do not pass `inputForFunction` outside of the closure, use it inside.
//...
}
如果我需要从unsafe pointer转换为不安全的[mutable]指针,怎么做?
当你发现像
UnsafePointer<[Float]>
这样的东西时,你就走错了方向。在swift中,Array
是一个隐藏结构,它可能包含对其元素的(多级)间接引用。您通常不需要使用UnsafePointer<Array<Float>>
。如果需要,如何与MemoryRebound一起使用?我可以将地址/值分配给闭包内的inputforfunction吗?
当您有一个
AType
数组并希望将其作为Unsafe(Mutable)Pointer<AnotherType>
传递时,您可以利用withMemoryRebound
。但在您的情况下,您只需要使用Float
,因此您的目的可能不需要使用withMemoryRebound
。当我检查了浮点数组和浮点数组[0 ]的地址时,它们是不同的(这似乎不同于C/C++)。哪个地址应该传递给inputforfunction?
没有。正如我在上面写的,floatarray的地址只是指向一个包含隐藏结构的地方,所以对于您的目的来说,它是完全无用的。floatarray[0]的地址不能保证是整个内容的第一个元素的地址。swift可以创建一个只包含一个元素的时态区域,并传递该时态区域的地址。
顺便问一下,你真的需要把你的
[Float]
转换成Unsafe(Mutable)Pointer<Float>
吗?如果函数的签名是这样的:
func takingUnsafePointer(_ pointer: UnsafePointer<Float>)
你可以这样称呼它:
takingUnsafePointer(floatArray)
//In this case `floatArray` can be a `let` constant.
或者,如果函数签名是:
func takingUnsafeMutablePointer(_ pointer: UnsafeMutablePointer<Float>)
称之为:
takingUnsafeMutablePointer(&floatArray)
//In this case `floatArray` needs to be a `var`, not `let`.
记住,在这两种情况下,传递的指针只有在函数调用时才有效。不应将指针导出到其他位置。