我的问题是:如何在 python 中读取内存地址的内容? 例子: ptr = id(7) 我想读取ptr指向的内存内容。 谢谢。
最佳答案
看看ctypes.string_at .这是一个例子。它转储 CPython 整数的原始数据结构。
from ctypes import string_at
from sys import getsizeof
a = 0x7fff
print(string_at(id(a),getsizeof(a)).hex())
输出:
0200000000000000d00fbeaafe7f00000100000000000000ff7f0000
请注意,这适用于 CPython 实现,因为 id()
恰好返回 Python 对象的虚拟内存地址,但 Python 语言本身并不能保证这一点。
关于python - 在python中访问内存地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8250625/