sizeof
来源:互联网 发布:管材销售 知乎 编辑:程序博客网 时间:2024/06/05 23:50
定义: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;}
阅读全文
0 0
- SIZEOF
- Sizeof
- sizeof
- sizeof
- sizeof
- sizeof
- sizeof
- sizeof............
- sizeof
- sizeof
- sizeof
- sizeof
- sizeof
- sizeof
- sizeof
- sizeof
- sizeof
- sizeof
- 网络请求图片缓存
- Unix和Linux有什么区别?
- java:String使用equals和==比较的区别
- yii 权限RBAC
- bzoj4484[Jsoi2015]最小表示 拓补排序+bitset
- sizeof
- 应用标题及描述不可滥用关键字
- Java学习路线图(转载)
- 剑指offer面试题目:把数组排成最小的数
- hascode和equals为什么要同时复写—Java源码中的奥秘
- UVA 11624 Fire!
- chrome 谷歌 浏览器 更新后页面布局变大处理
- Docker 存储引擎
- String构造器中文手册