简体   繁体   中英

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

我读过关于numpy.frombuffer的内容,但找不到任何从指针创建数组的方法。

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM