struct sizeof result not expected(struct sizeof 结果不是预期的)
问题描述
我有一个这样定义的结构:
I have a a struct defined thusly:
typedef struct _CONFIGURATION_DATA {
BYTE configurationIndicator;
ULONG32 baudRate;
BYTE stopBits;
BYTE parity;
BYTE wordLength;
BYTE flowControl;
BYTE padding;
} CONFIGURATION_DATA;
现在,据我估计,该结构体有 10 个字节长.但是,sizeof 报告它是 16 字节长?有人知道为什么吗?
Now, by my reckoning, that struct is 10 bytes long. However, sizeof reports that it is 16 bytes long? Anyone know why?
我正在使用 Windows DDK 中的构建工具进行编译.
I am compiling using the build tools in the Windows DDK.
推荐答案
对齐.
使用
#pragma pack(1)
...结构在这里...
#pragma pack()
我还建议重新排序,如果有必要填充,然后使用 RESERVED 字节,以便多字节整数类型更好地对齐.这将使 tbe CPU 的处理速度更快,并且您的代码更小.
I would also recommend reordering things, and if necessary padding then with RESERVED bytes, so that multi-byte integral types will be better aligned. This will make processing faster for tbe CPU, and your code smaller.
这篇关于struct sizeof 结果不是预期的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:struct sizeof 结果不是预期的
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07