对于很多C
++新手而言,对象或变量的
sizeof信息总是让人捉摸不透,以下程序列举了几组典型的
sizeof信息,希望能解答大家在使用
sizeof时的疑问。
在列举这几个例子前需要说明以下几点:
1、在Win32平台上,指针长度都是 4字节, char *、 int *、 double *如此,vbptr ( virtual base table pointer )、vfptr ( virtual function table pointer )也是如此;
2、对于结构体 (或类 ),编译器会自动进行成员变量的对齐,以提高运算效率。自然对齐 (natural alignment )也称默认对齐方式是按结构体的成员中size最大的成员对齐的,强制指定大于自然对齐大小的对齐方式是不起作用的 (Specifying a packing level larger than the natural alignment of a type does not change the type alignment .)。
3、不推荐强制对齐,大量使用强制对齐会严重影响处理器的处理效率。
范例 1: (一个简单的C语言的例子 )
void f ( int arr [])
{
cout << "sizeof(arr) = " << sizeof (arr ) << endl ; //当被作为参数进行传递时,数组失去了其大小信息
}
void main ()
{
char szBuf [] = "abc" ;
cout << "sizeof(szBuf) = " << sizeof (szBuf ) << endl ; //输出数组占用空间大小
char * pszBuf = szBuf ;
cout << "sizeof(pszBuf) = " << sizeof (pszBuf ) << endl ; //输出的是指针的大小
int iarr [ 3 ]; iarr ;
cout << "sizeof(iarr) = " << sizeof (iarr ) << endl ; //输出数组占用空间大小
f (iarr );
int * piarr = iarr ;
cout << "sizeof(piarr) = " << sizeof (piarr ) << endl ; //输出指针的大小
}
范例 2: (一个涉及alignment的例子 )
struct DATA1
{
char c1 ; //偏移量0,累积size = 1
char c2 ; //偏移量1,累积size = 1 + 1 = 2
short si ; //偏移量2,累积size = 2 + 2
};
struct DATA2
{
char c1 ; //偏移量0,累积size = 1
short si ; //偏移量1 + (1),累积size = 1 + (1) + 2 = 4
char c2 ; //偏移量4,累积size = 4 + 1 = 5,但按最大长度sizeof(short) = 2对齐,故最后取6
};
struct DATA3
{
char c1 ; //偏移量0,累积size = 1
double d ; //偏移量1 + (7),累积size = 1 + (7) + 8 = 16
char c2 ; //偏移量16,累积size = 16 + 1 = 17,但按最大长度sizeof(double) = 8对齐,故最后取24
};
#pragma pack(push,1) //强制1字节对齐
struct DATA4
{
char c1 ; //偏移量0,累积size = 1
double d ; //偏移量1,累积size = 1 + 8 = 9
char c2 ; //偏移量9,累积size = 9 + 1 = 10
};
#pragma pack(pop) //恢复默认对齐方式
struct DATA5
{
char c1 ;
double d ;
char c2 ;
};
void main ()
{
cout << "sizeof(DATA1) = " << sizeof (DATA1 ) << endl ;
cout << "sizeof(DATA2) = " << sizeof (DATA2 ) << endl ;
cout << "sizeof(DATA3) = " << sizeof (DATA3 ) << endl ;
cout << "sizeof(DATA4) = " << sizeof (DATA4 ) << endl ;
cout << "sizeof(DATA5) = " << sizeof (DATA5 ) << endl ;
}
范例 3: (C ++语言特征对 sizeof的影响 )
class CA
{
};
class CB : public CA
{
public :
void func () {}
};
class CC : virtual public CA
{
};
class CD
{
int k ; //私有成员
public :
CD () {k = - 1 ;}
void printk () { cout << "k = " << k << endl ; }
};
class CE : public CD
{
};
class CF
{
virtual void func () {}
};
void main ()
{
cout << "sizeof(CA) = " << sizeof (CA ) << endl ; //为了区分不包含任何成员的类的不同的元素,编译器会自动为类添加一个匿名元素
cout << "sizeof(CB) = " << sizeof (CB ) << endl ; //与上面类似,编译器也为CB添加了一个匿名元素
cout << "sizeof(CC) = " << sizeof (CC ) << endl ; //虚拟继承中vbptr(virtual base table pointer)占用4个字节
cout << "sizeof(CD) = " << sizeof (CD ) << endl ;
cout << "sizeof(CE) = " << sizeof (CE ) << endl ; //访问权限控制是在编译期间由编译器控制的,所以虽然不能访问CD类的成员k,这里仍然占用了sizeof(int)大小的空间
//下面的代码进一步说明上述观点,由于在复杂的类层次结构中,当涉及到虚函数或者虚拟继承等时,有些信息是运行期动态生成的,故请勿效仿以下方法对对象进行修改
CE e ;
e .printk ();
memset (&e , 0 , sizeof (CE ));
e .printk (); //从这里可以看出,上面的memset操作修改了CD类的私有成员k
cout << "sizeof(CF) = " << sizeof (CF ) << endl ; //虚函数表指针占用4个字节
}
在列举这几个例子前需要说明以下几点:
1、在Win32平台上,指针长度都是 4字节, char *、 int *、 double *如此,vbptr ( virtual base table pointer )、vfptr ( virtual function table pointer )也是如此;
2、对于结构体 (或类 ),编译器会自动进行成员变量的对齐,以提高运算效率。自然对齐 (natural alignment )也称默认对齐方式是按结构体的成员中size最大的成员对齐的,强制指定大于自然对齐大小的对齐方式是不起作用的 (Specifying a packing level larger than the natural alignment of a type does not change the type alignment .)。
3、不推荐强制对齐,大量使用强制对齐会严重影响处理器的处理效率。
范例 1: (一个简单的C语言的例子 )
void f ( int arr [])
{
cout << "sizeof(arr) = " << sizeof (arr ) << endl ; //当被作为参数进行传递时,数组失去了其大小信息
}
void main ()
{
char szBuf [] = "abc" ;
cout << "sizeof(szBuf) = " << sizeof (szBuf ) << endl ; //输出数组占用空间大小
char * pszBuf = szBuf ;
cout << "sizeof(pszBuf) = " << sizeof (pszBuf ) << endl ; //输出的是指针的大小
int iarr [ 3 ]; iarr ;
cout << "sizeof(iarr) = " << sizeof (iarr ) << endl ; //输出数组占用空间大小
f (iarr );
int * piarr = iarr ;
cout << "sizeof(piarr) = " << sizeof (piarr ) << endl ; //输出指针的大小
}
范例 2: (一个涉及alignment的例子 )
struct DATA1
{
char c1 ; //偏移量0,累积size = 1
char c2 ; //偏移量1,累积size = 1 + 1 = 2
short si ; //偏移量2,累积size = 2 + 2
};
struct DATA2
{
char c1 ; //偏移量0,累积size = 1
short si ; //偏移量1 + (1),累积size = 1 + (1) + 2 = 4
char c2 ; //偏移量4,累积size = 4 + 1 = 5,但按最大长度sizeof(short) = 2对齐,故最后取6
};
struct DATA3
{
char c1 ; //偏移量0,累积size = 1
double d ; //偏移量1 + (7),累积size = 1 + (7) + 8 = 16
char c2 ; //偏移量16,累积size = 16 + 1 = 17,但按最大长度sizeof(double) = 8对齐,故最后取24
};
#pragma pack(push,1) //强制1字节对齐
struct DATA4
{
char c1 ; //偏移量0,累积size = 1
double d ; //偏移量1,累积size = 1 + 8 = 9
char c2 ; //偏移量9,累积size = 9 + 1 = 10
};
#pragma pack(pop) //恢复默认对齐方式
struct DATA5
{
char c1 ;
double d ;
char c2 ;
};
void main ()
{
cout << "sizeof(DATA1) = " << sizeof (DATA1 ) << endl ;
cout << "sizeof(DATA2) = " << sizeof (DATA2 ) << endl ;
cout << "sizeof(DATA3) = " << sizeof (DATA3 ) << endl ;
cout << "sizeof(DATA4) = " << sizeof (DATA4 ) << endl ;
cout << "sizeof(DATA5) = " << sizeof (DATA5 ) << endl ;
}
范例 3: (C ++语言特征对 sizeof的影响 )
class CA
{
};
class CB : public CA
{
public :
void func () {}
};
class CC : virtual public CA
{
};
class CD
{
int k ; //私有成员
public :
CD () {k = - 1 ;}
void printk () { cout << "k = " << k << endl ; }
};
class CE : public CD
{
};
class CF
{
virtual void func () {}
};
void main ()
{
cout << "sizeof(CA) = " << sizeof (CA ) << endl ; //为了区分不包含任何成员的类的不同的元素,编译器会自动为类添加一个匿名元素
cout << "sizeof(CB) = " << sizeof (CB ) << endl ; //与上面类似,编译器也为CB添加了一个匿名元素
cout << "sizeof(CC) = " << sizeof (CC ) << endl ; //虚拟继承中vbptr(virtual base table pointer)占用4个字节
cout << "sizeof(CD) = " << sizeof (CD ) << endl ;
cout << "sizeof(CE) = " << sizeof (CE ) << endl ; //访问权限控制是在编译期间由编译器控制的,所以虽然不能访问CD类的成员k,这里仍然占用了sizeof(int)大小的空间
//下面的代码进一步说明上述观点,由于在复杂的类层次结构中,当涉及到虚函数或者虚拟继承等时,有些信息是运行期动态生成的,故请勿效仿以下方法对对象进行修改
CE e ;
e .printk ();
memset (&e , 0 , sizeof (CE ));
e .printk (); //从这里可以看出,上面的memset操作修改了CD类的私有成员k
cout << "sizeof(CF) = " << sizeof (CF ) << endl ; //虚函数表指针占用4个字节
}