Constructing an array from memory address

Travis Oliphant oliphant at
Tue Oct 10 19:36:49 CDT 2006

Daniel Drake wrote:

>I have an area of memory which is shared between processes (it is
>actually a shared memory segment). The address of this memory is stored
>in a python long variable, which I pass to various custom C/C++ python
>I would like to construct a numpy array in this area. Is there any way I
>can tell numpy to use a specific address (stored as a long) to use as
>storage for the array? 

This functionality is already available.   There are two ways to do it. 

1) Create an object with an __array_interface__ attribute that returns a 
dictionary with shape, typestr, and data fields.   The data field should 
return the tuple  (memory-address-as-long, 

Passing this object into the asarray constructor will use the memory given

2) Use the asbuffer function in numpy.core.multiarray  (it's not exposed 
to numpy yet) to create a buffer object from the memory address and then 
use frombuffer to create an array from the memory.

Good luck,


Take Surveys. Earn Cash. Influence the Future of IT
Join's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash

More information about the Numpy-discussion mailing list