定义:sizeof是Pascal的一种内存容量度量函数。C语言中判断数据类型或者表达式长度的运算符;不是一个函数,字节(byte)数的计算在程序编译时进行,而不是在程序执行的过程中才计算出来。
1:字节、位、字长
字 word
字节 byte
位 bit
1字=2字节(1 word = 2 byte)
1字节=8位(1 byte = 8bit)
一个字的字长为16(bit)
一个字节的字长是8(bit)
2.基本类型
2.1:整型
int main()
{
short a,aa[10];
int b ,bb[10];
long c,cc[10];
long long d,dd[10];
cout << "sizeof(a)=" << sizeof(a) << endl;
cout << "sizeof(b)=" << sizeof(b) << endl;
cout << "sizeof(c)=" << sizeof(c) << endl;
cout << "sizeof(d)=" << sizeof(d) << endl;
cout << "sizeof(aa)=" << sizeof(aa) << endl;
cout << "sizeof(bb)=" << sizeof(bb) << endl;
cout << "sizeof(cc)=" << sizeof(cc) << endl;
cout << "sizeof(dd)=" << sizeof(dd) << endl;
}
2.2:浮点型
int main()
{
float a,aa[10];
double b,bb[10];
long double c,cc[10];
cout << "sizeof(a)=" <<sizeof(a) << endl;
cout << "sizeof(b)=" << sizeof(b) << endl;
cout << "sizeof(c)=" << sizeof(c) << endl;
cout << "sizeof(aa)=" << sizeof(aa) << endl;
cout << "sizeof(bb)=" << sizeof(bb) << endl;
cout << "sizeof(cc)=" << sizeof(cc) << endl;
}
2.3:字符
int main ()
{
char a,aa[10];
cout << "sizeof(a)=" << sizeof(a) << endl;
cout << "sizeof(aa)=" << sizeof(aa) << endl;
}
2.4:布尔
int main()
{
bool a, aa[10];
cout << "sizeof(a)=" << sizeof(a) << endl;
cout << "sizeof(aa)=" << sizeof(aa) << endl;
}
3:指针
3.1:整型
int main()
{
short *a,*aa[10];
int *b ,*bb[10];
long *c,*cc[10];
long long *d,*dd[10];
cout << "sizeof(a)=" <<sizeof(a) << endl;
cout << "sizeof(b)=" << sizeof(b) << endl;
cout << "sizeof(c)=" << sizeof(c) << endl;
cout << "sizeof(d)=" << sizeof(d) << endl;
cout << "sizeof(aa)=" << sizeof(aa) << endl;
cout << "sizeof(bb)=" << sizeof(bb) << endl;
cout << "sizeof(cc)=" << sizeof(cc) << endl;
cout << "sizeof(dd)=" << sizeof(dd) << endl;
}
3.2:浮点型
int main ()
{
float *a,*aa[10];
double *b,*bb[10];
long double *c,*cc[10];
cout << "sizeof(a)=" <<sizeof(a) << endl;
cout << "sizeof(b)=" << sizeof(b) << endl;
cout << "sizeof(c)=" << sizeof(c) << endl;
cout << "sizeof(aa)=" << sizeof(aa) << endl;
cout << "sizeof(bb)=" << sizeof(bb) << endl;
cout << "sizeof(cc)=" << sizeof(cc) << endl;
}
3.3:字符型
int main()
{
char *a,*aa[10];
cout << "sizeof(a)=" << sizeof(a) << endl;
cout << "sizeof(aa)=" << sizeof(aa) << endl;
}
3.4:布尔型
int main()
{
bool *a, *aa[10];
cout << "sizeof(a)=" << sizeof(a) << endl;
cout << "sizeof(aa)=" << sizeof(aa) << endl;
}
4:结构体和类
4.1:结构体
#include <stdio.h>
#include <iostream>
using namespace std;
typedef struct
{
char a:3;
char b:3;
char c:3;
char d:3;
char e:3;
}test1;
typedef struct
{
char a:3;
char b:4;
char c:5;
char d:6;
char e:7;
}test2;
typedef struct
{
char a:1;
char b:2;
char c:3;
char d:4;
char e:5;
}test3;
typedef struct{
int a;
char b;
char d;
long c;
}test4;
typedef struct{
int a;
char b;
long c;
char d;
}test5;
int main()
{
cout<<sizeof(test1)<<endl;
cout<<sizeof(test2)<<endl;
cout<<sizeof(test3)<<endl;
cout<<sizeof(test4)<<endl;
cout<<sizeof(test5)<<endl;
return 0;
}
4.2:类
#include <stdio.h>
#include <iostream>
using namespace std;
class emptyClass1{
public:
emptyClass1(){}
~emptyClass1(){}
};
class emptyClass2{
public:
emptyClass2(){}
virtual ~emptyClass2(){}
};
class hwcBase{
public:
hwcBase(){}
virtual ~hwcBase(){}
private:
int base;
};
class hwcSubFirst :hwcBase{
public:
hwcSubFirst() :hwcBase(){}
~hwcSubFirst(){}
private:
int sub;
};
class hwcSubSecond :hwcBase{
public:
hwcSubSecond() :hwcBase(){}
~hwcSubSecond(){}
private:
int sub;
char sub2;
};
int main()
{
cout << sizeof(emptyClass1) << endl;
cout << sizeof(emptyClass2) << endl;
cout << sizeof(hwcBase) << endl;
cout << sizeof(hwcSubFirst) << endl;
cout << sizeof(hwcSubSecond) << endl;
system("pause");
return 0;
}