python - 在python中访问内存地址

标签 python memory ctypes memory-address

我的问题是:如何在 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/

相关文章:

python - 安装 python 模块自定义位置 PYTHONUSERBASE/virtualenv

python - 将 Python 列表变量复制到现有 xlsx excel 文件中

javascript - django ajax请求获取选定的索引

c# - 获取内存中 DataTable 或 ArrayList 的大小

python - 在 cython 函数中使用 python ctypes 数组数据

python - 将 ctypes 与 cuda 结合使用,在脚本末尾出现段错误(核心转储)错误

python - django-dashing 未正确设置

java - Java中如何在位级别内部表示整数?

node.js - NodeJS/ExpressJS 内存泄漏

ctypes - 使用 ctypes 将元组的元组从 c 返回到 python