如何转换UnsafePointer&lt; [Float]&gt;或者在Swift 3中将Float Array数据拟合到Unsafe [Mutable]指针<float>?

时间:2016-12-01 09:09:40

标签: swift pointers memory-management unsafe-pointers unsafemutablepointer

我有一个来自HDF5文件的浮点值数组(floatArray),我希望将此数据用作接受Unsafe [Mutable]指针的函数的输入({{1 }})。我是UnsafePointer的新手,所以我需要别人的帮助。

我想到的一种方法是,将数组存储到文件中,然后将其打开并将文件描述符映射到变量的内存中以输入函数:

inputForFunction

但是,它可能不是最佳实现,因为存储数据的过程看起来很耗时。

这就是我考虑使用UnsafePointer将指针从 // 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") } 传递到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
  • 如果我需要从UnsafePointer&lt; [Float]&gt;转换到不安全[Mutable]指针,怎么做?
  • 如果需要,如何将inputForFunction用于此目的?我可以分配 封闭内的withMemoryRebound的地址/值?
  • 当我检查inputForFunctionfloatArray的地址时,它们是不同的(这似乎与C / C ++不同)。哪个地址应传递给floatArray[0]

2 个答案:

答案 0 :(得分:2)

您可以找到许多文章,说明如何从Unsafe(Mutable)Pointer<Float>获取[Float]。他们中的一些人回答了你的一些问题。试试以后再找。

  • 如何将floatArray数据的地址传递给inputForFunction?

    您通常使用数组的方法withUnsafeBufferPointerwithUnsafeMutableBufferPointer。您可以在closure参数中获得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.
        //...
    }
    
  • 如果我需要从UnsafePointer&lt; [Float]&gt;转换到不安全[Mutable]指针,怎么做?

    当你找到像UnsafePointer<[Float]>这样的东西时,你就会走错路。在Swift中,Array是一个隐藏的结构,可能包含对其元素的(多级)间接引用。您通常无需使用UnsafePointer<Array<Float>>

  • 如果需要,如何使用withMemoryRebound来实现此目的?我可以将地址/值分配给闭包内的inputForFunction吗?

    当您拥有AType数组且希望将其作为Unsafe(Mutable)Pointer<AnotherType>传递时,您可以使用withMemoryRebound。但在您的情况下,您只需使用Float,因此您的目的可能不需要withMemoryRebound

  • 当我检查floatArray和floatArray [0]的地址时,它们是不同的(这似乎与C / C ++不同)。应该将哪个地址传递给inputForFunction?

    无。正如我上面所写, floatArray 的地址只是指向一个包含隐藏结构的地方,所以它对你的目的来说完全没用。并且floatArray [0] 的地址既不保证是整个内容的第一个元素的地址。 Swift可以创建一个仅包含一个元素的时间区域,并传递时间区域的地址。

顺便说一下,您真的需要将[Float]转换为Unsafe(Mutable)Pointer<Float>吗?

如果你的功能&#39;签名是这样的:

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`.

请记住,在这两种情况下,传递的指针仅在函数调用时有效。您不应该将指针导出到其他位置。

答案 1 :(得分:1)

Array类型有withUnsafeBufferPointer方法。

此方法接受UnsafeBufferPointer的关闭,就像您要求的UnsafePointer<[Float]>一样。

如果您需要UnsafePointer到数组的开头,则可以使用UnsafeBufferPointer的{​​{3}}属性。

示例代码:

let bufferPointer: UnsafeBufferPointer<Float> = floatArray.withUnsafeBufferPointer { bufferPointer in
    return bufferPointer
}

let baseAddress: UnsafePointer<Float> = floatArray.withUnsafeBufferPointer { bufferPointer in
    return bufferPointer.baseAddress
}

编辑:或者只是传递&floatArray的功能可能有效。