python - Python ctypes.sizeof(...) 总是返回 0
问题描述
根据我对 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
为什么会这样?
解决方案
你忘记加下划线_
了_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))
输出:
16
16
推荐阅读
- java - 是否可以更改 TimePicker 默认文本?
- python - regex to separate items in a list
- excel - 如果目标单元格时间(格式为 hh:mm:ss)在某个范围内,则活动单元格显示文本
- javascript - 类中“滚动”事件的 removeEventListener
- node.js - 我没有找到 BeaconParserEddystone 方法
- java - 访问相关实体时的JPA和NullPointerException,但随机
- webdriver-manager - 将 WebDriverManager 用于 selenium 时出现 WebDriverManagerException
- sql-server - 使用 case 语句更新数据类型
- javascript - 为子数组中的每个条目添加值
- ssl - 禁用不安全侦听端口时,Weblogic 10.3.6 托管服务器无法启动