python中如何查看指定内存地址的内容

本文介绍在Python中如何查看指定内存地址的内容,适用于熟悉C/C++的开发者。通过内置函数和模块,我们可以获取对象的内存大小、内存地址,并以16进制形式展示内存布局。示例中展示了如何对不同类型的对象进行操作,但并非所有对象都支持此功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

博客:博客园 | 优快云 | blog

python中一般并不需要查看内存内容,但作为从C/C++过来的人,有的时候还是想看看内存,有时是为了验证内容是否与预期一致,有时是为了探究下内存布局。

from sys import getsizeof 
from ctypes import string_at

'''
getsizeof(...)
    getsizeof(object, default) -> int
    Return the size of object in bytes.
    
string_at(ptr, size=-1)
    string_at(addr[, size]) -> string
    Return the string at addr.
'''

getsizeof用于获取对象占用的内存大小,string_at用于获取指定地址、指定字节长度的内容,因为返回的对象类型是bytes,可以调用hex()函数转换成16进制查看。

int对象的内存内容如下,首先通过函数id获取对象的内存地址。

i = 100
type(i)
# int
s = string_at(id(i), getsizeof(i))
type(s)
# bytes
s
# b'>\x00\x00\x00\x00\x00\x00\x00\xa0\x99\xfd\x1d\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00d\x00\x00\x00'
s.hex()
# '3e00000000000000a099fd1d00000000010000000000000064000000'

如果对int对象的内存布局不熟悉,可能看不出什么。

再举一个numpy的例子。

>>> import numpy as np
>>> a = np.arange(12).reshape(3,4)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

>>> a.data
<memory at 0x00000000062483A8>
>>> m = a.data
>>> type(m)
memoryview
>>> m.hex()
'000000000100000002000000030000000400000005000000060000000700000008000000090000000a0000000b000000'

>>> a.ctypes.data
68393696
>>> string_at(a.ctypes.data, a.nbytes).hex()
'000000000100000002000000030000000400000005000000060000000700000008000000090000000a0000000b000000'


上面展示的两个例子,一个是通过memoryview对象查看,另一个是通过string_at查看。不是所有对象都支持memoryview

class memoryview(obj)

Create a memoryview that references obj. obj must support the buffer protocol. Built-in objects that support the buffer protocol include bytes and bytearray.

—— from https://docs.python.org/3/library/stdtypes.html#memoryview

string_at

ctypes.string_at(address, size=-1)

This function returns the C string starting at memory address address as a bytes object. If size is specified, it is used as size, otherwise the string is assumed to be zero-terminated.

—— from https://docs.python.org/3/library/ctypes.html?highlight=string_at#ctypes.string_at

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值