sizeof运算符来获取各种数据类型在内存中所占字节数--gyy整理
2016-03-03 17:50
429 查看
C++并没有规定各种数据类型在内存中的存储大小,依赖于不同的编译器的不同而不同,要想获知当前编译器对各种数据类型分配的大小,可以通过sizeof运算符来获取。
使用方法1:
sizeof(数据类型)
使用方法2:
sizeof(变量名 或 常量名 或 表达式 )
sizeof(int)
或
int a;
sizeof(a)
[cpp] view plain copy
//数据类型空间分配情况
#include <iostream>
using namespace std;
int main()
{
cout<<"vc++6.0 编译环境下,各种数据类型变量所占的内存空间大小(字节为单位)"<<endl;
cout<<"sizeof(int) "<<sizeof(int)<<endl;
cout<<"sizeof(short int) "<<sizeof(short)<<endl;
cout<<"sizeof(long int) "<<sizeof(long)<<endl;
cout<<"sizeof(unsigned int) "<<sizeof(unsigned)<<endl;
cout<<"sizeof(unsigned short int) "<<sizeof(unsigned short)<<endl;
cout<<"sizeof(unsigned long int) "<<sizeof(unsigned long)<<endl;
cout<<"sizeof(char ) "<<sizeof(char)<<endl;
cout<<"sizeof(float) "<<sizeof(float)<<endl;
cout<<"sizeof(double) "<<sizeof(double)<<endl;
cout<<"sizeof(long double) "<<sizeof(long double)<<endl;
return 0;
}
运行结果
使用方法1:
sizeof(数据类型)
使用方法2:
sizeof(变量名 或 常量名 或 表达式 )
sizeof(int)
或
int a;
sizeof(a)
[cpp] view plain copy
//数据类型空间分配情况
#include <iostream>
using namespace std;
int main()
{
cout<<"vc++6.0 编译环境下,各种数据类型变量所占的内存空间大小(字节为单位)"<<endl;
cout<<"sizeof(int) "<<sizeof(int)<<endl;
cout<<"sizeof(short int) "<<sizeof(short)<<endl;
cout<<"sizeof(long int) "<<sizeof(long)<<endl;
cout<<"sizeof(unsigned int) "<<sizeof(unsigned)<<endl;
cout<<"sizeof(unsigned short int) "<<sizeof(unsigned short)<<endl;
cout<<"sizeof(unsigned long int) "<<sizeof(unsigned long)<<endl;
cout<<"sizeof(char ) "<<sizeof(char)<<endl;
cout<<"sizeof(float) "<<sizeof(float)<<endl;
cout<<"sizeof(double) "<<sizeof(double)<<endl;
cout<<"sizeof(long double) "<<sizeof(long double)<<endl;
return 0;
}
运行结果
相关文章推荐
- PHP实现登录,注册,密码修改
- springmvc学习笔记(一)之简介
- (15)字典操作
- python qrcode 库的使用
- 检查form表单数据是否发生变化
- 简析Android的垃圾回收与内存泄露
- c++ 总结小基础
- 一款消消乐游戏的自动解法
- Java基础知识——Java集合详解
- DOS命令
- Mac系统下下删除加锁文件方法|使用终端命令强制清除废纸篓中的文件
- C#处理VC++动态库回调函数的字符串指针参数(传递二进制流)
- lvs+keepalived
- 阿里2015实习生-客户端笔试题目解析
- Flume采集rsyslog发送的audit日志
- lumped port&wave port/ driven modal 与driven terminal
- 移动互联网测试
- Browserify
- 跑马灯效果
- 短信通信笔记