最近我试图从 python2 转移到 python3,在我的代码中有一些关于从硬件读取数据的工作,这些硬件有一个 .py 接口文件调用外部 .dll 库。数据在.dll和python例程之间由内存共享,具体来说,ctypes.creat_string_buffer()和ctypes.addressof()在python2.7环境下运行正常,但在python3.6下却出现意外结果,原因似乎是 ctypes.addressof() 给出了巨大的差异地址值,我想知道是什么原因?
'''python2.7的addressof()输出
(base) C:\Users\Administrator>python
Python 2.7.15 |Anaconda, Inc.| (default, May 1 2018, 18:37:09) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> s = 128
>>> p = create_string_buffer(s)
>>> print(addressof(p))
50341488
>>> hex(addressof(p))
'0x3002670L'
'''
'''python3.6的addressof()输出
(base) C:\Users\Administrator>conda activate py36
(py36) C:\Users\Administrator>python
Python 3.6.8 |Anaconda, Inc.| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> s = 128
>>> p = create_string_buffer(s)
>>> print(addressof(p))
>>> 2241150277680
>>> hex(addressof(p))
>>> '0x209cef75830'
'''
在我看来,python2和python3下addressof()函数的输出应该是近似的,但实际上并非如此。有人可以帮助我指出例程有什么问题,或者与我一起,不胜感激!