NumPy frombuffer giving nonsense values when reading C float array on Windows

urschrei at gmail.com urschrei at gmail.com
Tue Jul 26 08:06:19 EDT 2016


I'm using ctypes to interface with a binary which returns a void pointer (ctypes c_void_p) to a nested 64-bit float array:
[[1.0, 2.0], [3.0, 4.0], … ]
then return the pointer so it can be freed

I'm using the following code to de-reference it:

# a 10-element array
shape = (10, 2)
array_size = np.prod(shape)
mem_size = 8 * array_size
array_str = ctypes.string_at(ptr, mem_size)
# convert to NumPy array,and copy to a list
ls = np.frombuffer(array_str, dtype="float64", count=array_size).reshape(shape).tolist()
# return pointer so it can be freed
drop_array(ptr)
return ls

This works correctly and consistently on Linux and OSX using NumPy 1.11.0, but fails on Windows 32 bit and 64-bit about 50% of the time, returning nonsense values. Am I doing something wrong? Is there a better way to do this?

--
s




More information about the Python-list mailing list