问题
i just need to alloc memory & set values there.Here is my code.
let radius:Float = 5.79
let sigma:Float = radius / 2
let size:Int = Int((round(radius) * 2) + 1)
var weights:UnsafeMutableRawPointer = malloc(MemoryLayout<Float>.size * size * size)
weights[some index] = some vale
but I am getting an error message in swift version 4 saying "UnsafeMutableRawPointer has no subscript members"
How can I fixed this.Any ideas please
回答1:
You should better check the official documentation of UnsafeMutableRawPointer
You can write something like this:
let radius:Float = 5.79
let sigma:Float = radius / 2
let size:Int = Int((round(radius) * 2) + 1)
var weights:UnsafeMutableRawPointer = malloc(MemoryLayout<Float>.size * size * size)
weights.storeBytes(of: some value, toByteOffset: some offset, as: SomeType.self)
Or you should better use UnsafeMutablePointer<Float> instead, if all the elements are Float.
var weights:UnsafeMutablePointer<Float> = UnsafeMutablePointer.allocate(capacity: size * size)
weights[some index] = some vale
来源:https://stackoverflow.com/questions/52733309/getting-error-message-of-type-unsafemutablerawpointer-has-no-subscript-members