#include<iostream>
using namespace std;
struct information
{
float f;
int i;
char c;
};
struct info
{
double b;
char c;
};
int main()
{
double b;
char c;
cout << "sizeof( struct information) = " << sizeof ( struct information) << endl;
cout << "sizeof( struct info) = " << sizeof( struct info) << endl;
cout << "sizeof(b) = " << sizeof b << endl;
cout << "sizeof (c) = " << sizeof c << endl;
return 0;
}
运行结果是:
sizeof( struct information) = 12
sizeof( struct info) = 16
sizeof(b) = 8
sizeof (c) = 1
请按任意键继续. . .
其结果是:
sizeof(class base) = 8
sizeof(class derived)=24
请按任意键继续. . .
关于内存对齐还有一点要注意的是:
sizeof不计算static变量,函数不占内存,但是虚函数就占内存了,而且无论多少虚函数,都只占4个字节。Just because there haveonly one vptr.