关于一个结构体位对齐的有关问题
关于一个结构体位对齐的问题
下面这个程序期望a数组里为0x00_D7_FF_FA_00(结构体共存储38位,补两位0),但是不对,请高手指点一下
#include <iostream>
#include <windows.h>
using namespace std;
struct GOP //38 bits
{
WORD a:10 __attribute__((packed));
BYTE b:3 __attribute__((packed));
WORD c __attribute__((packed));
BYTE d:1 __attribute__((packed));
BYTE e:3 __attribute__((packed));
BYTE f:1 __attribute__((packed));
BYTE g:3 __attribute__((packed));
BYTE h:1 __attribute__((packed));
};
int main()
{
BYTE a[5] = {0};
GOP gp;
//00_D7_FF_FA_00
//0000_0000_11 01_0 111_1111_1111_1111_1 0 10_0 0 00_0 0 00
gp.a = 3;
gp.b = 2;
gp.c = 0xFFFF;
gp.d = 0;
gp.e = 4;
gp.f = 0;
gp.g = 0;
gp.h = 0;
memcpy(a,&gp,sizeof(gp));
for (int i=0; i <5; i++ )
printf( "%x ",a[i]);
}
------解决方案--------------------
little-end & big-end的问题?
下面这个程序期望a数组里为0x00_D7_FF_FA_00(结构体共存储38位,补两位0),但是不对,请高手指点一下
#include <iostream>
#include <windows.h>
using namespace std;
struct GOP //38 bits
{
WORD a:10 __attribute__((packed));
BYTE b:3 __attribute__((packed));
WORD c __attribute__((packed));
BYTE d:1 __attribute__((packed));
BYTE e:3 __attribute__((packed));
BYTE f:1 __attribute__((packed));
BYTE g:3 __attribute__((packed));
BYTE h:1 __attribute__((packed));
};
int main()
{
BYTE a[5] = {0};
GOP gp;
//00_D7_FF_FA_00
//0000_0000_11 01_0 111_1111_1111_1111_1 0 10_0 0 00_0 0 00
gp.a = 3;
gp.b = 2;
gp.c = 0xFFFF;
gp.d = 0;
gp.e = 4;
gp.f = 0;
gp.g = 0;
gp.h = 0;
memcpy(a,&gp,sizeof(gp));
for (int i=0; i <5; i++ )
printf( "%x ",a[i]);
}
------解决方案--------------------
little-end & big-end的问题?