获取ctypes指针指向的内存地址 [英] Get the memory address pointed to by a ctypes pointer

查看:363
本文介绍了获取ctypes指针指向的内存地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

短版:
我如何获得ctypes指针指向的地址?

Short version: How can I get the address that a ctypes pointer points to?

长版:
我已经注册了python用作C库的回调。 C库期望 void(* p_func)(char stat,char * buf,short buf_len)的函数签名,因此我注册了一个适当的python函数。当我进入python函数时,我想知道 buf 指向的内存地址。我该怎么做?

Long version: I have registered a python function as a callback with a C library. The C library expects function signature of void (*p_func)(char stat, char * buf, short buf_len) so I register an appropriate python function. When I get into the python function, I want to know the memory address pointed to by buf. How can I do this?

推荐答案

我已经通过阅读文档解决了这个问题。

I have fixed this myself by reading the documentation.

我想知道由库分配的一块内存的存储位置。我有指向该块的ctypes指针。为了获得块的内存地址,我使用了 ctypes.addressof(p_block.contents)

I wanted to know the memory location of a block of memory allocated by a library. I had the ctypes pointer that pointed to said block. To get the memory address of the block I used ctypes.addressof(p_block.contents).

根据我的理解, p_block.contents!= p_block.contents ,但是后来我意识到所有 p_block.contents 对象都有相同的基础缓冲区。基础缓冲区的地址是通过 ctypes.addressof 获得的。

The confusion arose around my understanding that p_block.contents != p_block.contents, but then I realised all p_block.contents objects have the same underlying buffer. The address of the underlying buffer is obtained with ctypes.addressof.

这篇关于获取ctypes指针指向的内存地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆