Mano-Wii
Mano-Wii

Reputation: 592

Constructing an array from memory address

I have done this function that uses ctypes to create an object with a buffer protocol that points to a specified address:

import numpy as np

def np_buffer_from_address(shape, dtype, address):
    import ctypes
    return np.ndarray(shape, dtype = dtype, buffer = ctypes.c_void_p.from_address(address))

But I wanted to know if I can do this without using ctypes. If you can do it directly with numpy

Upvotes: 2

Views: 2240

Answers (1)

lomereiter
lomereiter

Reputation: 356

You can avoid ctypes by using just the standardized __array_interface__, here's an example how it works: Creating a NumPy array directly from __array_interface__ (in fact, numpy.ctypeslib.as_array does the same under the hood, setting typestr and data appropriately)

Upvotes: 2

Related Questions