对于不同的c/c++编译器会采取不同的处理方式
下面举一个例子
#include <stdio.h>
class A
{
public:
int a;
short b;
int c;
char d;
};
class B
{
public:
double a;
short b;
int c;
char d;
};
int main()
{
A a;
B b;
printf("sizeof A : %d\n" , sizeof(A));
printf("class A member:\n");
printf("a : 0x%x\n" , &(a.a));
printf("b : 0x%x\n" , &(a.b));
printf("c : 0x%x\n" , &(a.c));
printf("d : 0x%x\n" , &(a.d));
printf("end of class A\n");
printf("sizeof B : %d\n" , sizeof(B));
printf("class B member:\n");
printf("a : 0x%x\n" , &(b.a));
printf("b : 0x%x\n" , &(b.b));
printf("c : 0x%x\n" , &(b.c));
printf("d : 0x%x\n" , &(b.d));
printf("end of class B\n");
return 0;
}
windows x64位下运行结果
gcc编译
sizeof A : 16
class A member:
a : 0x28ff10
b : 0x28ff14
c : 0x28ff18
d : 0x28ff1c
end of class A
sizeof B : 24
class B member:
a : 0x28fef8
b : 0x28ff00
c : 0x28ff04
d : 0x28ff08
end of class B
vc6.0 cl编译器
sizeof A : 16
class A member:
a : 0x18ff38
b : 0x18ff3c
c : 0x18ff40
d : 0x18ff44
end of class A
sizeof B : 24
class B member:
a : 0x18ff20
b : 0x18ff28
c : 0x18ff2c
d : 0x18ff30
end of class B
但是在centos 6.5 i686 上gcc 编译
说明在不同编译器上对double和int 即4字节和8字节处理是不同的。