1

I am trying to get an UnsafeMutableBufferPointer using the following code it works sometimes in Playground and it fails also

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
}
6
  • "I am trying to get UnsafeMutableBufferPointer" Can you explain why? It might help to know what you are really trying to accomplish. Commented Jul 10, 2016 at 15:26
  • @matt I am playing with swift I want to compare looping using UnsafeMutableBufferPointer and the simple for in enumeration Commented Jul 10, 2016 at 15:28
  • @matt do you have any idea how to achieve that ? Commented Jul 10, 2016 at 15:33
  • But what do you want to compare? An array is not an unsafe mutable buffer pointer, so you would be comparing apples and oranges. Commented Jul 10, 2016 at 15:54
  • On the matter of speed in accessing array elements, you might want to see my answer here: stackoverflow.com/a/36160922/341994 Commented Jul 10, 2016 at 16:20

2 Answers 2

7

Memory addressed by UnsafeMutablePointer can be in one of three states:

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

The call

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

allocates memory, but does not initialize it:

/// 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)

On the other hand, the subscripting methods require that the pointee is initialized:

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

As a consequence, your code crashes inside _swift_release_.

To initialize the allocated memory from the (character) array, you can use

memory.initializeFrom(array)

Of course you must de-initialize and deallocate the memory eventually.


A different approach is

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

Here no new memory is allocated, but the closure is called with a pointer to the arrays contiguous storage. This buffer pointer is only valid inside the closure.

Sign up to request clarification or add additional context in comments.

1 Comment

Yup, and it crashes while doing a release because when replacing the value with a subscript, ARC will try to deallocate the previous reference type instance that was contained at that location. His code would have worked using a struct (with no ref inside) or other value types.
2

It seems likely that you are looking for Array's withUnsafeBufferPointer method. This gives you direct access to the array's contiguous in-memory storage. You might want to start with something like this:

    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
    }

2 Comments

Note that this will crash for an empty array as the repeat will be evaluated before the while – you could just use a for loop instead.
@originaluser2 Agreed, I just wanted to give something to get started with, as I said. I don't actually have any notion what, if anything, the OP wants to accomplish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.