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

  1. 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_NULL

    This example wraps a native pointer ptr as a 1-dimensional array of type Cvoid with 5 elements.

  2. 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_NULL

    Here, the native pointer ptr is wrapped as a 2-dimensional array of type Cvoid with dimensions (3, 2).

  3. 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_NULL

    In this example, the true value is passed as the take_ownership argument, 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.

*Required Field
Details

Checking you are not a robot: