有一个结构_dem_ram_data_。它应该在内存中占用 16 个字节,但我得到 21 个字节。我在 64 位系统上使用它。
#include <iostream>
typedef unsigned long ViUInt32;
typedef long ViInt32;
typedef unsigned char ViUInt8;
typedef unsigned long long ViUInt64;
#pragma pack(push,1)
typedef struct _dem_ram_data_{
union {
struct {
ViUInt32 errors : 32;
ViInt32 addr : 27;
ViUInt8 _reserve : 2;
ViUInt8 dataType : 2;
ViUInt8 graphEna : 1;
} vecAddr;
struct {
ViUInt64 tact : 42;
ViUInt32 _reserve : 19;
ViUInt8 dataType : 2;
ViUInt8 graphEna : 1;
} tact;
ViUInt64 d;
} demData;
ViUInt32 cpl;
ViUInt32 cph;
} DemRamData, *PDemRamData;
#pragma pack(pop)
using namespace std;
int main()
{
cout << sizeof(DemRamData) << endl;
return 0;
}
该问题通过将不同类型的位字段替换为一个来解决。谢谢!