Skip to content Skip to sidebar Skip to footer

Ctypes C++ Function Returns Array Of Unknown Size

I have a c++ function that accepts a Pointer to an array of known size as input and returns an array of size that cannot be determined until the function completes its processing o

Solution 1:

Normally it is preferable to get the caller to allocate the buffer. That way the caller is in a position to deallocate it also. However, in this case, only the callee knows how long the buffer needs to be. And so the onus passes to the callee to allocate the buffer.

But that places an extra constraint on the system. Since the callee is allocating the buffer, the caller cannot deallocate it unless they share the same allocator. That can actually be arranged without too much trouble. You can use a shared allocator. There are a few. Your platform appears to be Windows, so for example you can use CoTaskMemAlloc and CoTaskMemFree. Both sides of the interface can call those functions.

The alternative is to keep allocation and deallocation together. The caller must hold on to the pointer that the callee returns. When it has finished with the buffer, usually after copying it into a Python structure, it asks the library to deallocate the memory.

Solution 2:

David gave you useful advice on the memory management concerns. I would generally use the simpler strategy of having a function in the library to free the allocated buffer. The onus is on the caller to prevent memory leaks.

To me your question seems to be simply about casting the result to the right pointer type. Since you have the length in index 0, you can set the result type to a long long pointer. Then get the size so you can cast the result to the correct pointer type.

def callout(largeListUlonglongs):
    cFunc = PyDLL("./cFunction.dll").processNumbers
    cFunc.restype = POINTER(c_ulonglong)

    arrayOfNums = c_ulonglong * len(largeListUlonglongs)
    numsArray = arrayOfNums(*largeListUlonglongs)   

    result = cFunc(numsArray)
    size = result[0]
    returnArray = cast(result, POINTER(c_ulonglong * size))[0]

    return returnArray

Post a Comment for "Ctypes C++ Function Returns Array Of Unknown Size"