题目1:
struct test{
char a; 1+3
int b; 4
} Test;
sizeof(Test) = 8;
题目2:
struct test{
char a; 1+3
int b; 4
char c; 1+3(补齐)
}Test;
sizeof(Test) = 12;
题目3:
struct test {
char a[18]; 18 + 6 (18不是8的倍数,加6)
double b; 8
char c; 1 +3(1不是4的倍数,加3)
int d; 4
short e; 2 +6(2不是8的倍数,前面cd已形成8字节组合,所以自己应加6)
}Test;
sizeof(Test) = 48
题目4:
struct test{
char a; 1+7
double b; 8
char c; 1+3
int d; 4
}Test;
sizeof(Test) = 24;
题目5:
struct test{
char a; 1+7
double b; 8
short c; 2+6
}Test;
sizeof(Test) = 24;
题目6:
struct test{
char a; 1 + 3
int b; 4
double c; 8
char d; 1+7
}Test;
sizeof(Test) = 24;
字节对齐: