Python ctypes.sizeof(...)始终返回0

时间:2018-07-27 22:02:31

标签: python python-2.7 ctypes

据我对Python ctypes的了解,ctypes.sizeof(...)应该返回传入的结构的大小(以字节为单位),就像使用C的sizeof运算符一样。但是,我总是得到0作为结果:

$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> class testStruct(ctypes.Structure):
...     _fields = [
...         ("testField", ctypes.c_uint*4)
...     ]
...
>>> ctypes.sizeof(testStruct)
0
>>> test = testStruct()
>>> ctypes.sizeof(test)
0

为什么会这样?

1 个答案:

答案 0 :(得分:3)

您忘了在:plugins [[javax.xml.bind/jaxb-api "2.3.0"] [lein-gorilla "0.4.0"]]) 下加_底线,应该是_fields

_fields_

输出:

import ctypes
class testStruct(ctypes.Structure):
    # NOT just _fields:
    _fields_ = [
        ("testField", ctypes.c_uint*4)
    ]

print(ctypes.sizeof(testStruct))
test = testStruct()
print(ctypes.sizeof(test))
相关问题