简体   繁体   English

如何获取 C 的 numpy 数组的 memory 地址

[英]how to get the memory address of a numpy array for C

I constructed an numpy array::我构造了一个 numpy 数组::

a=np.ndarray([2,3]) 

then i want to see where its data are::然后我想看看它的数据在哪里::

a.data 
>>>Out[213]: <read-write buffer for 0x0482C1D0, size 48, offset 0 at 0x049E87A0> 
a.data 
>>>Out[214]: <read-write buffer for 0x0482C1D0, size 48, offset 0 at 0x049E82A0> 
a.data 
>>>Out[215]: <read-write buffer for 0x0482C1D0, size 48, offset 0 at 0x049E81C0> 

... ...

why every time the offset address is different?为什么每次偏移地址都不一样? if i want to transfer the data to a c function using c_types by::如果我想使用 c_types 将数据传输到 c function :

ctypes_array = (ctypes.c_char * a.size * 8).from_address(ptr) 

how should i get the value of ptr?我应该如何获得 ptr 的值?

Also, have a look at ndarray.__array_interface__ , which is a dict that contains all of the information you're after. 另外,看一下ndarray.__array_interface__ ,这是一个包含你所有信息的字典。

In your case, 在你的情况下,

pointer, read_only_flag = a.__array_interface__['data']

a.data might be a property whose getter function creates a new buffer object (meta data) on each call. a.data可能是一个属性,其getter函数在每次调用时创建一个新的缓冲区对象(元数据)。

To get the address see how numpy.ctypeslib.as_ctypes() is implemented. 要获取地址,请参阅如何实现numpy.ctypeslib.as_ctypes()

NumPy currently has documented interface to get raw pointer address to an array like this: NumPy 目前有文档化的接口来获取数组的原始指针地址,如下所示:

a = np.asarray([2, 3])
address = a.ctypes.data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM