Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #111893

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

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: NumPy frombuffer giving nonsense values when reading C float array on Windows
Date 2016-07-26 16:20 +0200
Organization None
Message-ID <mailman.127.1469542860.22221.python-list@python.org> (permalink)
References <4481593b-69fe-4314-aca8-50b783847212@googlegroups.com> <nn7rjt$e2j$1@ger.gmane.org>

Show all headers | View raw


urschrei@gmail.com wrote:

> 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?

I'd verify that the underlying memory has not been freed by the "binary" 
when you are doing the ctypes/numpy processing. You might get the correct 
values only when you are "lucky" and the memory has not yet been reused for 
something else, and you are "lucky" on Linux/OSX more often than on 
Windows...

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

NumPy frombuffer giving nonsense values when reading C float array on Windows urschrei@gmail.com - 2016-07-26 05:06 -0700
  Re: NumPy frombuffer giving nonsense values when reading C float array on Windows Peter Otten <__peter__@web.de> - 2016-07-26 16:20 +0200
    Re: NumPy frombuffer giving nonsense values when reading C float array on Windows sth <urschrei@gmail.com> - 2016-07-26 08:09 -0700
      Re: NumPy frombuffer giving nonsense values when reading C float array on Windows Christian Gollwitzer <auriocus@gmx.de> - 2016-07-26 17:36 +0200
        Re: NumPy frombuffer giving nonsense values when reading C float array on Windows sth <urschrei@gmail.com> - 2016-07-26 09:47 -0700
  Re: NumPy frombuffer giving nonsense values when reading C float array on Windows eryk sun <eryksun@gmail.com> - 2016-07-26 18:09 +0000
    Re: NumPy frombuffer giving nonsense values when reading C float array on Windows sth <urschrei@gmail.com> - 2016-07-26 11:31 -0700
      Re: NumPy frombuffer giving nonsense values when reading C float array on Windows eryk sun <eryksun@gmail.com> - 2016-07-26 22:42 +0000

csiph-web