This question already has an answer here:
How to dereference a memory location from python ctypes?

(1个答案)


5年前关闭。




我有一个类似于0x6041f0的地址。我知道那里有一个整数。在C语言中,我只需完成*(int *)0x6041f0即可获取该地址处的整数值。

如何在Python中实现相同的目标?

PS:我正在编写一个使用gdb模块的Python脚本。实际调试的程序是C++。因此,需要进行许多底层操作。

最佳答案

像这样的东西:

$ python
Python 2.7.9 (default, Mar 19 2015, 22:32:11)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> c_int_p = POINTER(c_int)
>>> x = c_int(4)
>>> cast(addressof(x), c_int_p).contents
c_int(4)

用那个任意地址:)
>>> cast(0x6041f0, c_int_p)
<__main__.LP_c_int object at 0x7f44d06ce050>
>>> cast(0x6041f0, c_int_p).contents
Segmentation fault

请参阅:ctypes以供引用

关于python - 如何在python中将地址解引用为整数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30813787/

10-14 18:20
查看更多