【问题标题】:How to get an UnsafeMutableBufferPointer<Character> from array of Character如何从字符数组中获取 UnsafeMutableBufferPointer<Character>
【发布时间】:2016-07-10 15:24:56
【问题描述】:

我正在尝试使用以下代码获取 UnsafeMutableBufferPointer,它有时在 Playground 中有效,但也失败了

let array : [Character] = ....
func getUnsafeMP(array: [Character]) -> UnsafeMutableBufferPointer<Character> {

    let count = array.count
    let memory = UnsafeMutablePointer<Character>(allocatingCapacity: count)

    for (index , value) in array.enumerated() {

        memory[index] = value //it fails here EXC_BAD_ACCESS
    }

    let buffer = UnsafeMutableBufferPointer(start: memory, count: count)

    return buffer
}

【问题讨论】:

  • "我正在尝试获取 UnsafeMutableBufferPointer" 你能解释一下为什么吗?了解您真正想要完成的工作可能会有所帮助。
  • @matt 我正在玩 swift 我想比较使用 UnsafeMutableBufferPointer 和简单的 for in enumeration 的循环
  • @matt 你知道如何实现吗?
  • 但是你想比较什么?数组不是一个不安全的可变缓冲区指针,所以你会比较苹果和橘子。
  • 关于访问数组元素的速度问题,您可能想在这里看到我的回答:stackoverflow.com/a/36160922/341994

标签: swift swift3 unsafemutablepointer


【解决方案1】:

UnsafeMutablePointer 寻址的内存可以位于以下位置之一 三种状态:

/// - Memory is not allocated (for example, pointer is null, or memory has
///   been deallocated previously).
///
/// - Memory is allocated, but value has not been initialized.
///
/// - Memory is allocated and value is initialised.

电话

let memory = UnsafeMutablePointer<Character>(allocatingCapacity: count)

分配内存,但不初始化它:

/// Allocate and point at uninitialized aligned memory for `count`
/// instances of `Pointee`.
///
/// - Postcondition: The pointee is allocated, but not initialized.
public init(allocatingCapacity count: Int)

另一方面,下标方法要求指针被初始化:

/// Access the pointee at `self + i`.
///
/// - Precondition: the pointee at `self + i` is initialized.
public subscript(i: Int) -> Pointee { get nonmutating set }

因此,您的代码在 _swift_release_ 内崩溃。

从(字符)数组初始化分配的内存, 你可以使用

memory.initializeFrom(array)

当然,你最终必须取消初始化和释放内存。


另一种方法是

var cArray: [Character] = ["A", "B", "C"]
cArray.withUnsafeMutableBufferPointer { bufPtr  in
    // ...
}

这里没有分配新的内存,但是调用了闭包 指向数组连续存储的指针。这个缓冲区指针 仅在闭包内有效。

【讨论】:

  • 是的,它在释放时崩溃,因为用下标替换值时,ARC 将尝试释放包含在该位置的先前引用类型实例。他的代码可以使用结构(内部没有 ref)或其他值类型。
【解决方案2】:

您似乎正在寻找 Array 的 withUnsafeBufferPointer 方法。这使您可以直接访问阵列的连续内存存储。你可能想从这样的事情开始:

    let arr = Array("hello there".characters)
    arr.withUnsafeBufferPointer { p -> Void in
        var i = 0
        repeat {
            print("character:", p[i])
            i += 1
        } while i < p.count
    }

【讨论】:

  • 请注意,这将导致空数组崩溃,因为 repeat 将在 while 之前进行评估 - 您可以只使用 for 循环。
  • @originaluser2 同意,正如我所说,我只是想提供一些开始的东西。我实际上不知道 OP 想要完成什么(如果有的话)。
猜你喜欢
  • 2011-04-24
  • 2011-05-31
  • 2019-01-05
  • 2015-03-26
  • 2021-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
相关资源
最近更新 更多