struct sizeof结果不期望 [英] struct sizeof result not expected

查看:122
本文介绍了struct sizeof结果不期望的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如此定义的结构:

  typedef struct _CONFIGURATION_DATA {
BYTE configurationIndicator;
ULONG32 baudRate;
BYTE stopBits;
BYTE parity;
BYTE wordLength;
BYTE flowControl;
BYTE padding;
} CONFIGURATION_DATA;



现在,根据我的推算,该结构体是10个字节长。然而,sizeof报告,它是16个字节长?任何人都知道为什么?



我正在使用Windows DDK中的构建工具进行编译。

解决方案

比对。



使用



#pragma pack )



... struct到这里...



#pragma pack()



我还建议重新排序,并且如果必要的话用RESERVED字节填充,这样多字节整数类型将更好地对齐。这将使CPU的处理速度更快,您的代码更小。


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;

Now, by my reckoning, that struct is 10 bytes long. However, sizeof reports that it is 16 bytes long? Anyone know why?

I am compiling using the build tools in the Windows DDK.

解决方案

Alignment.

use

#pragma pack(1)

...struct goes here...

#pragma pack()

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结果不期望的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆