python数组地址传递给C

标签 python c arrays

我想将 python 数组缓冲区的地址传递给另一个应用程序或 c 函数,

buf = array("d", range(10))
addr, count = buf.buffer_info()
print "Address of buffer ", addr

输出:

Address of buffer  0x28a3ab0

这个地址是buf数组第一个元素的实际内存地址吗?

这个地址是否与 C 中的类似,即 buf 的第一个元素的地址?

如果不是,我如何在 python 中声明一个缓冲区并将其地址传递给 c 函数并像 C 数组一样使用它的值和寻址?

如果这个地址是实际的内存地址,我可以在 C 中的 memcpy() 函数中使用这个地址。

最佳答案

the documentation写了,用它作为地址就可以了。但考虑使用 buffer protocol根据注释。

Return a tuple (address, length) giving the current memory address and the length in elements of the buffer used to hold array’s contents. The size of the memory buffer in bytes can be computed as array.buffer_info()1 * array.itemsize. This is occasionally useful when working with low-level (and inherently unsafe) I/O interfaces that require memory addresses, such as certain ioctl() operations. The returned numbers are valid as long as the array exists and no length-changing operations are applied to it.

Note When using array objects from code written in C or C++ (the only way to effectively make use of this information), it makes more sense to use the buffer interface supported by array objects. This method is maintained for backward compatibility and should be avoided in new code. The buffer interface is documented in Buffer Protocol.

关于python数组地址传递给C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40701483/

相关文章:

php - 如何在php中使用数组键和while循环来显示mysql表数据

php - 我可以使用复选框消除数据库中的最后一行

python - Scrapy - 解析页面以提取项目 - 然后跟踪并存储项目 url 内容

python - 从 __init__ 调用类方法而不重复类名

python - 绘制国债 yield 曲线,如何使用 matplotlib 叠加两条 yield 曲线

将 RAM 地址转换为指向结构的指针的理解困惑

python - 从列表中迭代删除(Python 2)

关于在 C 中为具有偏移量的指针赋值的困惑

在 C 中为 malloc 和 free 创建一个包装函数

ios - 获取一个 NSArray