#pragma pack(2) 按照2个字节进行对齐
#pragma pack(4) 按照4个字节进行对齐
不写,就按照编译器进行字节对齐
#include <stdio.h>
#include <string.h>
using namespace std;
# pragma pack(4)
struct ta{
unsigned char a; // 1 Byte
unsigned long long b; // 8 Byte
unsigned short c; // 2 Byte
char a1[3];
};
# pragma pack(2)
struct tb{
unsigned char a; // 1 Byte
unsigned long long b; // 8 Byte
unsigned short c; // 2 Byte
char a1[5];
};
void dump( void *t, int size){
printf("---- @%x size = %2d ----\n", ( int)t, size);
for ( int i = 0; i < size; ++i){
printf("%2d: %x\n", i, *(( char*)t+i));
}
printf("-------------------\n");
}
int main(){
ta a;
memset(&a, 0, sizeof(ta));
a.a = 0xff;
a.b = 0xffffffffffffffffull;
a.c = 0xffff;
dump(&a, sizeof(ta));
tb b;
memset(&b, 0, sizeof(tb));
b.a = 0xff;
b.b = 0xffffffffffffffffull;
b.c = 0xffff;
dump(&b, sizeof(tb));
return 0;
}