关于结构体中变量类型顺序对结构体大小的影响疑问。
不知道大家有没有遇到过这种情况:
#include <iostream>
using namespace std;
class A{
char a;
char c;
int b;
};
void main()
{
A a;
cout<<sizeof(A)<<endl;
cout<<sizeof(a)<<endl;
}
输出结果为:
8
8
==================================================
#include <iostream>
using namespace std;
class A{
char a;
int b;
char c;
};
void main()
{
A a;
cout<<sizeof(A)<<endl;
cout<<sizeof(a)<<endl;
}
输出结果:
12
12
其中把int变量放在最前边和放在最后边结果一样,但是放在中间结果却不一样,这如何解释啊?