1

I am writing a code, where I create an numpy array of pointers. They point to other arrays.

I can succesfully (no exception is generated) store a pointer inside one element of the array-of-pointers. But I cannot re-convert this pointer to a numpy array.

The problem specifically appears when pointers are stored in an numpy array of pointers. I can succesfully store and retrive an array when I store the pointer in a normal pyhon variable.

Note that I cannot just create a python list of pointers because of performances reasons.

this code works:

import numpy, ctypes
ic = numpy.array([[1,2],[3,4]],dtype=numpy.int32)
pointer = ic.__array_interface__['data'][0]
v = numpy.ctypeslib.as_array(ctypes.cast(pointer,ctypes.POINTER(ctypes.c_int)),shape=(2,2))
print(v)

and v will return the initial array set in ic.

this code does not work:

import numpy, ctypes
ic = numpy.array([[1,2],[3,4]],dtype=numpy.int32)
pointers = numpy.zeros(5, dtype=ctypes.POINTER(ctypes.c_int))
pointers[0] = ic.__array_interface__['data'][0]
numpy.ctypeslib.as_array(ctypes.cast(pointers[0],ctypes.POINTER(ctypes.c_int)),shape=(2,2))

The last line will give the following exception:

File "/opt/intel/intelpython3/lib/python3.5/ctypes/__init__.py", line 484, in cast
return _cast(obj, obj, typ)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type

Question: how do I store and retrive a numpy-array from/to a numpy-array of pointers?

1 Answer 1

2

Indexing the array returns a np.int32 object, not a native Python int:

In [118]: type(pointer)
Out[118]: int
In [119]: type(pointers[0])
Out[119]: numpy.int32

Use item to extract the int:

In [120]: type(pointers[0].item())
Out[120]: int

You could also first convert the array to list

In [121]: type(pointers.tolist()[0])
Out[121]: int

pointers as you construct it is a np.int32 dtype

In [123]: pointers = numpy.zeros(5, dtype=ctypes.POINTER(ctypes.c_int))
In [124]: pointers.dtype
Out[124]: dtype('int32')

Alternatively make a object dtype array

In [125]: pointers = numpy.zeros(5, dtype=object)
In [126]: pointers[0] = pointer
In [127]: pointers
Out[127]: array([157379928, 0, 0, 0, 0], dtype=object)
In [128]: type(pointers[0])
Out[128]: int
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.