in how to get the memory address of a numpy array for C is also used __array_interface__
, the integer that is returned is a pointer to the data segment of the array and no memory address :
data (optional)
A 2-tuple whose first argument is an integer (a long integer if necessary) that points to the data-area storing the array contents.
This pointer must point to the first element of data (in other words
any offset is always ignored in this case). The second entry in the
tuple is a read-only flag (true means the data area is read-only).
This attribute can also be an object exposing the buffer interface which will be used to share the data. If this key is not present (or
returns None), then memory sharing will be done through the buffer
interface of the object itself. In this case, the offset key can be
used to indicate the start of the buffer. A reference to the object
exposing the array interface must be stored by the new object if the
memory area is to be secured.
Default: None
source : https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.interface.html
you can see the content of the entire dictionary returned by __array_interface__
with print x.__array_interface__[]
__array_struct__
contains a distinct C pointer to the first element of the array, you can access it from within your C(++) code, example is in http://blog.audio-tk.com/2008/11/04/exposing-an-array-interface-with-swig-for-a-cc-structure/:
`
C-struct access
This approach to the array interface allows for faster access to an
array using only one attribute lookup and a well-defined C-structure.
__array_struct__
A :c:type: PyCObject whose voidptr member contains a pointer to a filled PyArrayInterface structure. Memory for the structure is
dynamically created and the PyCObject is also created with an
appropriate destructor so the retriever of this attribute simply has
to apply Py_DECREF to the object returned by this attribute when it is
finished. Also, either the data needs to be copied out, or a reference
to the object exposing this attribute must be held to ensure the data
is not freed. Objects exposing the __array_struct__
interface must
also not reallocate their memory if other objects are referencing
them.
The PyArrayInterface structure is defined in numpy/ndarrayobject.h
as:
typedef struct {
int two; /* contains the integer 2 -- simple sanity check */
int nd; /* number of dimensions */
char typekind; /* kind in array --- character code of typestr */
int itemsize; /* size of each element */
int flags; /* flags indicating how the data should be interpreted */
/* must set ARR_HAS_DESCR bit to validate descr */
Py_intptr_t *shape; /* A length-nd array of shape information */
Py_intptr_t *strides; /* A length-nd array of stride information */
void *data; --------> /* A pointer to the first element of the array */ < ------
PyObject *descr; /* NULL or data-description (same as descr key
of __array_interface__) -- must set ARR_HAS_DESCR
flag or this will be ignored. */
} PyArrayInterface;
source : https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.interface.html