Recently I'm trying to transfer from python2 to python3,in my codes there is some job about read data form hardware that have a .py interface file call foreign .dll lib. The data is shared by memory between .dll and python routine,specifically speaking, ctypes.creat_string_buffer() and ctypes.addressof(), which run correctly under python2.7 env,but give unexpected result under python3.6, the reason seems to be that ctypes.addressof() give huge difference address value, I wonder what's the reason?
'''python2.7 output of 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 output of 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'
'''
In my opinion,the output of addressof() funtion under python2 and python3 should be approximate,but in fact it's not。Some one who can help me to point what's wrong with the routine,or with me , appreciatively!
[Python 3.Docs]: ctypes - A foreign function library for Python.
ctypes.addressofdoesn't have anything to do with the values, it simply reports the address of the underlying C buffer for the ctypes object.It's about:
ctypes.create_string_buffer(which returns actypes.c_chararray)Output:
As seen, the memory addresses pattern is not related to ctypes objects, but to all (and the address is different every time).
Note that relying on the fact that an address pointing to some memory that has been allocated by malloc (or friends) is equal (or close) to some value, is a bad idea. Code relying on such assumptions, is bad (might say that's the equivalent of Undefined Behavior).