user1685095
user1685095

Reputation: 6121

How to create n-dim numpy array from a pointer?

I've read about numpy.frombuffer, but can't find any way to create array from pointer.

Upvotes: 12

Views: 13383

Answers (1)

ebarr
ebarr

Reputation: 7842

As pointed out in the comments above, you can use numpy.ctypeslib.as_array:

numpy.ctypeslib.as_array(obj, shape=None)

Create a numpy array from a ctypes array or a ctypes POINTER. The numpy array shares the memory with the ctypes object.

The size parameter must be given if converting from a ctypes POINTER. The size parameter is ignored if converting from a ctypes array

So let's mimic a C function returning a pointer with a call to malloc:

import ctypes as C
from ctypes.util import find_library
import numpy as np

SIZE = 10

libc = C.CDLL(find_library('c'))
libc.malloc.restype = C.c_void_p

# get a pointer to a block of data from malloc
data_pointer = libc.malloc(SIZE * C.sizeof(C.c_int))
data_pointer = C.cast(data_pointer,C.POINTER(C.c_int))

You can now make the data this pointer points to available to numpy

new_array = np.ctypeslib.as_array(data_pointer,shape=(SIZE,))

And to prove that they are accessing the same memory:

new_array[:] = range(SIZE)

print "Numpy array:",new_array[:SIZE]
print "Data pointer: ",data_pointer[:SIZE]

should output:

Numpy array: [0 1 2 3 4 5 6 7 8 9]
Data pointer:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

As a final note remember that the numpy array does not own its memory so explicit calls to free are required to avoid memory leaks.

del new_array
libc.free(data_pointer)

Upvotes: 22

Related Questions