计算struct大小,首先要让当前语句其数据类型大小的整数倍,如int a;此时应为4,
char c;
int i;
这个大小 为1+5->8;
最后的大小默认应为整个结构体中最长数据类型的整数倍
#include <iostream>
using namespace std;
struct data {
int a;//4字节
double b;//8字节
char c;//1字节
} ;
struct stu3
{
char c1;// 偏移量为0符合要求
int i; // 偏移量为1, 结构体变量中成员的偏移量必须是成员大小的整数倍(0被认为是任何数的整数倍),故偏移量应为4
char c2;// 偏移量为8(偏移量4+int大小4),符合要求
};
int main()
{
cout << sizeof(data) << endl; //0+4, (0+4+8)->16为8整数倍,16+1=17->24,24%8=0
cout << sizeof(stu3) << endl;//1,1+4->8,8+1->12
system("pause");
return 0;
}