Jas*_*zun 2 python ctypes python-2.7
根据我对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
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况?
你忘了加下划线_
,_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))
Run Code Online (Sandbox Code Playgroud)
输出:
16
16
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3346 次 |
最近记录: |