本文主要对C/C++中的sizeof关键字进行总结
1. 基本数据类型
cout << "--------------------------------------------------" << endl;
cout << "C/C++中的基本数据类型长度:" << endl;
cout << "sizeof(char) = " << sizeof(char) << endl;
cout << "sizeof(unsigned char) = " << sizeof(unsigned char) << endl;
cout << "sizeof(int) = " << sizeof(int) << endl;
cout << "sizeof(unsigned int) = " << sizeof(unsigned int) << endl;
cout << "sizeof(unsigned short) = " << sizeof(unsigned short) << endl;
cout << "sizeof(long) = " << sizeof(long) << endl;
cout << "sizeof(unsigned long) = " << sizeof(unsigned long) << endl;
cout << "sizeof(float) = " << sizeof(float) << endl;
cout << "sizeof(double) = " << sizeof(double) << endl;
cout << "sizeof(long double) = " << sizeof(long double) << endl;
测试结果如下:
--------------------------------------------------
C/C++中的基本数据类型长度:
sizeof(char) = 1
sizeof(unsigned char) = 1
sizeof(int) = 4
sizeof(unsigned int) = 4
sizeof(unsigned short) = 2
sizeof(long) = 8
sizeof(unsigned long) = 8
sizeof(float) = 4
sizeof(double) = 8
sizeof(long double) = 16
2. 稍微复杂点的结构
int *p = NULL;
cout << "-------------------------------------------" << endl;
cout << "比较特殊的对象size测试:" << endl;
cout << "sizeof(p) = " << sizeof(p) << endl;
cout << "sizeof(*p) = " << sizeof(*p) << endl;
int a[100];
cout << "int a[100], sizeof(a) = " << sizeof(a) << endl;
cout << "int a[100], sizeof(a[100]) = " << sizeof(a[100]) << endl;
cout << "int a[100], sizeof(&a) = " << sizeof(&a) << endl;
cout << "int a[100], sizeof(&a[0] = " << sizeof(&a[0]) << endl;
测试结果:
-------------------------------------------
比较特殊的对象size测试:
sizeof(p) = 8
sizeof(*p) = 4
int a[100], sizeof(a) = 400 //真个数组的内存占用
int a[100], sizeof(a[100]) = 4 //a[100]是int类型的
int a[100], sizeof(&a) = 8 //地址
int a[100], sizeof(&a[0] = 8 //a[0]的地址
3. 结构体struct和枚举类型enum
//测试enum
enum Color{
RED = 0x1,
BLACK,
BLUE,
YELLOW = 0x10,
WHITE
} union_variable_name;
//测试空结构体
struct Empty
{
};
struct S1
{
int age;
char gender;
double score;
};
struct S2
{
int age;
double score;
char gender;
};
void check_enum_info()
{
cout << "--------------------------------------------------" << endl;
cout << "enum和空结构体的长度" << endl;
cout << "sizeof(enum) = " << sizeof(union_variable_name) << endl;
cout << "sizeof(empty struct) = " << sizeof(Empty) << endl;
cout << "sizeof(s1) = " << sizeof(S1) << endl;
cout << "sizeof(s2) = " << sizeof(S2) << endl;
}
测试结果:
enum和空结构体的长度
sizeof(enum) = 4
sizeof(empty struct) = 1
sizeof(s1) = 16
sizeof(s2) = 24
从s1和s2的测试结果来看,对于struct而言,不同的布局方式,内存占用也不同。对其规则如下:
(1)字节对齐在编译时就已经确定好,运行时内存占用大小不会再改变。
(2)每个元素都是按自己的大小对齐,结构的整体大小必须是每个元素的整数倍,不足的补空字节。
(3)在VC++中,可以加上#pragma(n),设置内存对齐。每个元素按照min(sizeof(元素),
n)的方式来对齐。
4.柔性数组对sizeof的影响
//测试柔性数组对sizeof的挑战
struct Flexible_array
{
int a;
int b[];
};
void flexible_array()
{
cout << "-------------------------------------------" << endl;
cout << "测试柔性数组对sizeof计算的影响:" << endl;
cout << "sizeof(Flexible_array) = " << sizeof(Flexible_array) << endl;
Flexible_array *array = (Flexible_array *)malloc((2 *
(sizeof(Flexible_array) + 100 * sizeof(int))));
array[0].a = 1;
array[0].b[0] = 11;
array[0].b[1] = 12;
cout << "array[0].a = " << array[0].a << endl;
cout << "array[0].b[0] = " << array[0].b[0] << endl;
cout << "array[0].b[1] = " << array[0].b[1] << endl;
array[1].a = 2;
array[1].b[0] = 21;
array[1].b[1] = 22;
cout << "array[1].a = " << array[1].a << endl;
cout << "array[1].b[0] = " << array[1].b[0] << endl;
cout << "array[1].b[1] = " << array[1].b[1] << endl;
}
int main()
{
flexible_array();
return 0;
}
测试结果如下:
sizeof(Flexible_array) = 4
array[0].a = 1
array[0].b[0] = 11
array[0].b[1] = 12
array[1].a = 2
array[1].b[0] = 21
array[1].b[1] = 22
由此可见数组b对结构体的大小没有影响。