Unsafe_wrap perform memory allocations

,

Here’s a minimal demo of how to construct an UnsafeArray from a Ptr:

julia> using UnsafeArrays

julia> let N = 10, T = Float64
           _ptr = Libc.malloc(sizeof(T) * N)
           ptr = convert(Ptr{T}, _ptr)
           
           A = UnsafeArray(ptr, (N,))
           A .= 1
           @show sum(A)

           Libc.free(ptr)
       end
sum(A) = 10.0

the important thing to know here is that you have to call UnsafeArray(::Ptr{T}, ::NTuple{N, Int}) to construct an UnsafeArray{T, N}. For some reason they don’t have a method for UnsafeArray{T, N}(::Ptr, dims...). That’s why I did the convert step in the above code.

I’d recommend that at least while debugging your code and learning to make it work, you run your code within julia. It’ll make it easier to get error messages that make sense and get people to help you.

See also Please read: make it easier to help you