pointer_to_array
pointer_to_array(pointer, dims[, take_ownership::Bool])
Wrap a native pointer as a Julia Array object. The pointer element type determines the array element type. own optionally specifies whether Julia should take ownership of the memory, calling free on the pointer when the array is no longer referenced.
Examples
-
Wrap a native pointer as a 1-dimensional array:
julia> ptr = C_NULL; julia> arr = pointer_to_array(ptr, 5) 5-element Array{Cvoid,1}: C_NULL C_NULL C_NULL C_NULL C_NULLThis example wraps a native pointer
ptras a 1-dimensional array of typeCvoidwith 5 elements. -
Wrap a native pointer as a multi-dimensional array:
julia> ptr = C_NULL; julia> arr = pointer_to_array(ptr, (3, 2)) 3×2 Array{Cvoid,2}: C_NULL C_NULL C_NULL C_NULL C_NULL C_NULLHere, the native pointer
ptris wrapped as a 2-dimensional array of typeCvoidwith dimensions (3, 2). - Specify ownership of the memory:
julia> ptr = C_NULL; julia> arr = pointer_to_array(ptr, 5, true) 5-element Array{Cvoid,1}: C_NULL C_NULL C_NULL C_NULL C_NULLIn this example, the
truevalue is passed as thetake_ownershipargument, indicating that Julia should take ownership of the memory associated with the pointer.
Common mistake example:
julia> ptr = C_NULL;
julia> arr = pointer_to_array(ptr, 0)
ERROR: Size of dimension cannot be zero for array creation.
In this case, the size of the dimension provided is zero, which leads to an error. Ensure that the dimensions provided are valid and greater than zero when using pointer_to_array.
See Also
Array, broadcast, cat, combinations, conj!, digits!, fieldnames, fill, fill!, last, length, maximum, minimum, ones, parent, parentindexes, partitions, permutations, pointer, pointer_to_array, promote_shape, rand!, reshape, scale, similar, sum, sum_kbn, takebuf_array, transpose!, vec, zeros,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.