Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail 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: Tue, 26 Jul 2016 16:20:44 +0200 Organization: None Lines: 32 Message-ID: References: <4481593b-69fe-4314-aca8-50b783847212@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: news.uni-berlin.de bhe332Ai55UlBtQGjmPNkwWw1c+Zzj5CRgEf4eZugYaQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'binary': 0.05; 'float': 0.05; 'processing.': 0.07; 'subject:Windows': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'underlying': 0.09; '"lucky"': 0.16; 'numpy': 0.16; 'osx': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:array': 0.16; 'subject:values': 0.16; 'subject:when': 0.16; 'wrote:': 0.16; 'memory': 0.17; 'else,': 0.18; 'nested': 0.18; 'pointer': 0.18; 'shape': 0.18; 'windows': 0.20; '64-bit': 0.22; 'ctypes': 0.22; 'void': 0.22; 'bit': 0.23; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'linux': 0.26; 'correct': 0.28; 'values': 0.28; 'interface': 0.29; 'array': 0.29; 'convert': 0.29; "i'm": 0.30; 'code': 0.30; "i'd": 0.31; 'values.': 0.33; 'correctly': 0.34; 'this?': 0.34; 'list': 0.34; 'returning': 0.35; 'something': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'doing': 0.38; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'email addr:gmail.com': 0.62; 'skip:n 10': 0.62; 'more': 0.63; '50%': 0.79; '\xe2\x80\xa6': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8c98.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <4481593b-69fe-4314-aca8-50b783847212@googlegroups.com> Xref: csiph.com comp.lang.python:111893 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...