Ctypes: fast way to convert a return pointer to an array or Python list
NickName:RTC222 Ask DateTime:2018-02-22T03:26:20

Ctypes: fast way to convert a return pointer to an array or Python list

I am using ctypes to pass an array pointer to a dll and return a pointer to an array of doubles that was created using malloc in the dll. On return to Python, I need a fast way to convert the pointer to an array or Python list.

I can use this list comp, but it's slow because there are 320,000 data points:

list_of_results = [ret_ptr[i] for i in range(320000)]

Ideally I would create the array in Python and pass it to the dll, but I have to create it using malloc in the dll because this is a dynamic array where I don't know beforehand how many data elements there will be (although the return pointer also returns the number of data elements, so I know how many there are on return to Python) -- I use realloc to extend the array size dynamically in the dll; I can use realloc with a Python array, but a call to free() at the end is not guaranteed to work.

Here is the relevant Python code:

CallTest = hDLL.Main_Entry_fn
CallTest.argtypes = [ctypes.POINTER(ctypes.c_double), ctypes.c_int64]
CallTest.restype = ctypes.POINTER(ctypes.c_double)
ret_ptr = CallTest(DataArray, number_of_data_points)
list_of_results = [ret_ptr[i] for i in range(320000)]

So my question is: what is the fastest way to convert a pointer returned from a dll to a Python list or array? The method shown above is too slow.

Copyright Notice:Content Author:「RTC222」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48913932/ctypes-fast-way-to-convert-a-return-pointer-to-an-array-or-python-list

More about “Ctypes: fast way to convert a return pointer to an array or Python list” related questions

Ctypes: fast way to convert a return pointer to an array or Python list

I am using ctypes to pass an array pointer to a dll and return a pointer to an array of doubles that was created using malloc in the dll. On return to Python, I need a fast way to convert the point...

Show Detail

Convert ctypes byte array to Python list of floating point numbers

I have allocated an array and cast it using the Python ctypes module: dataC = ctypes.cast(crt.malloc(size), ctypes.POINTER(ctypes.c_ubyte)) in order to get byte data from a C library: so...

Show Detail

How to convert a ctypes C pointer to a Numpy array?

I'm writing a callback function in Python: @ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, np.ctypeslib.ndpointer(dtype=cty...

Show Detail

How to convert ctypes pointer to ctypes array without copying data. (Not castingarray to pointer)

I have a python callback function which accepts 2 arguments: a pointer to a chunk of memory, and the length of the memory chunk. def my_callback( buf, len): """ buf is of type POINTER(c_ubyte...

Show Detail

How to get ctypes type object from an ctypes array

Actually, I'm trying to convert ctypes arrays to python lists and back. If found this thread. But it assumes that we know the type at compile time. But is it possible to retrieve a ctypes type fo...

Show Detail

Faster way to convert ctypes array to python list?

I was wondering if someone could advise me whether there is a better/faster approach to read data from my C program that outputs two lists of size n. I am using ctypes to call the C program. The l...

Show Detail

Return CTypes pointer from C

I'm writing a Python C Extension that needs to return a CTypes pointer to a char array in memory (I need to interface with another Python library that expects a CTypes pointer). I cannot find any

Show Detail

Portable/fast way to obtain a pointer to Numpy/Numpypy data

I recently tried PyPy and was intrigued by the approach. I have lots of C extensions for Python, which all use PyArray_DATA() to obtain a pointer to the data sections of numpy arrays. Unfortunately...

Show Detail

Ctypes array return elements as str not char pointers

I am trying to create a ctypes array which has char pointers as elements First I initialize char pointers to normal python list from ctypes import * array = [] for i in range(5): array.append(

Show Detail

Fill python ctypes pointer

I have a C function uint8_t *begin(); which returns a pointer to allocated memory. Here is a ctypes binding to it: begin = mylibrary.begin begin.argtypes = () begin.restype = ctypes.POINTER(ctypes.

Show Detail