C++基础 数据类型占字节大小分析
时间:2022-07-22
本文章向大家介绍C++基础 数据类型占字节大小分析,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
概述
C++基本数据类型
类型 |
关键字 |
---|---|
布尔型 |
bool |
字符型 |
char |
整型 |
int |
浮点型 |
float |
双浮点型 |
double |
无类型 |
void |
宽字符型 |
wchar_t |
一些基本类型可以使用一个或多个类型修饰符进行修饰:
- signed
- unsigned
- short
- long
各数据类型在内存中所占字节的大小随系统的差异而变,可通过sizeof()
函数查看,下面直接总结一些基本类型所占字节的大小。
所占字节大小总结
类型 |
32位系统 |
64位系统 |
---|---|---|
bool |
1 |
1 |
char |
1 |
1 |
short |
2 |
2 |
int |
4 |
4 |
long |
4 |
4 |
double |
8 |
8 |
float |
4 |
4 |
* |
4 |
8 |
注:有的地方说64位系统中long
占8字节,不过在我的电脑中实测确是4字节,所以还是要根据实际的硬件及编译器确定到底占用多少!
测试程序
几点说明:
- signed, unsigned, long和short都隐含了int, 等价于signed int, unsigned int, long int, short int
- 但注意char没有这种默认等价性。char不和signed char或者unsigned char其中任何一个等价。
如果用于文本,则使用未加限定的
char
, 是类似于'a'
,'0'
的类型, 或是组成C字符串"abcde"
的类型。它也可以是一个值,但是是当做无符号还是有符号数没有指定。如果用于数字,那么:signed char
, 范围 -128 到 127,unsigned char
, 范围 0 ~ 255。 - signed与signed int与int是等价类型
#include<iostream>
#include<string>
#include <limits>
using namespace std;
int main()
{
/*
signed, unsigned, long和short都隐含了int, 等价于signed int, unsigned int, long int, short int
但注意char没有这种默认等价性。char不和signed char或者unsigned char其中任何一个等价。使用char的时候最好标明是signed char还是unsigned char。
signed与signed int与int是等价类型
*/
cout << "type: tt" << "************size**************" << endl;
// *
cout << "--------------------------------" << endl;
int *p;
cout << "*p: tt" << "所占字节数:" << sizeof(p) << endl;
// bool
cout << "--------------------------------" << endl;
cout << "bool: tt" << "所占字节数:" << sizeof(bool);
cout << "t最大值:" << (numeric_limits<bool>::max)();
cout << "tt最小值:" << (numeric_limits<bool>::min)() << endl;
// char
cout << "--------------------------------" << endl;
cout << "char: tt" << "所占字节数:" << sizeof(char);
cout << "t最大值:" << (numeric_limits<char>::max)();
cout << "tt最小值:" << (numeric_limits<char>::min)() << endl;
cout << "signed char: t" << "所占字节数:" << sizeof(signed char);
cout << "t最大值:" << (numeric_limits<signed char>::max)();
cout << "tt最小值:" << (numeric_limits<signed char>::min)() << endl;
cout << "unsigned char: t" << "所占字节数:" << sizeof(unsigned char);
cout << "t最大值:" << (numeric_limits<unsigned char>::max)();
cout << "tt最小值:" << (numeric_limits<unsigned char>::min)() << endl;
// short (typedef short int wchar_t;)
cout << "--------------------------------" << endl;
cout << "short: tt" << "所占字节数:" << sizeof(short);
cout << "t最大值:" << (numeric_limits<short>::max)();
cout << "tt最小值:" << (numeric_limits<short>::min)() << endl;
cout << "unsigned short:t" << "所占字节数:" << sizeof(unsigned short);
cout << "t最大值:" << (numeric_limits<unsigned short>::max)();
cout << "tt最小值:" << (numeric_limits<unsigned short>::min)() << endl;
cout << "wchar_t: t" << "所占字节数:" << sizeof(wchar_t);
cout << "t最大值:" << (numeric_limits<wchar_t>::max)();
cout << "tt最小值:" << (numeric_limits<wchar_t>::min)() << endl;
// int
cout << "--------------------------------" << endl;
cout << "int: tt" << "所占字节数:" << sizeof(int);
cout << "t最大值:" << (numeric_limits<int>::max)();
cout << "t最小值:" << (numeric_limits<int>::min)() << endl;
cout << "unsigned: t" << "所占字节数:" << sizeof(unsigned);
cout << "t最大值:" << (numeric_limits<unsigned>::max)();
cout << "t最小值:" << (numeric_limits<unsigned>::min)() << endl;
// long
cout << "--------------------------------" << endl;
cout << "long: tt" << "所占字节数:" << sizeof(long);
cout << "t最大值:" << (numeric_limits<long>::max)();
cout << "t最小值:" << (numeric_limits<long>::min)() << endl;
cout << "unsigned long: t" << "所占字节数:" << sizeof(unsigned long);
cout << "t最大值:" << (numeric_limits<unsigned long>::max)();
cout << "t最小值:" << (numeric_limits<unsigned long>::min)() << endl;
// double
cout << "--------------------------------" << endl;
cout << "double: t" << "所占字节数:" << sizeof(double);
cout << "t最大值:" << (numeric_limits<double>::max)();
cout << "t最小值:" << (numeric_limits<double>::min)() << endl;
cout << "long double: t" << "所占字节数:" << sizeof(long double);
cout << "t最大值:" << (numeric_limits<long double>::max)();
cout << "t最小值:" << (numeric_limits<long double>::min)() << endl;
// float
cout << "--------------------------------" << endl;
cout << "float: tt" << "所占字节数:" << sizeof(float);
cout << "t最大值:" << (numeric_limits<float>::max)();
cout << "t最小值:" << (numeric_limits<float>::min)() << endl;
cout << "size_t: t" << "所占字节数:" << sizeof(size_t);
cout << "t最大值:" << (numeric_limits<size_t>::max)();
cout << "t最小值:" << (numeric_limits<size_t>::min)() << endl;
cout << "string: t" << "所占字节数:" << sizeof(string) << endl;
// << "t最大值:" << (numeric_limits<string>::max)() << "t最小值:" << (numeric_limits<string>::min)() << endl;
cout << "type: tt" << "************size**************" << endl;
return 0;
}
64位编译器结果(Visual Studio x64)
type: ************size**************
--------------------------------
*p: 所占字节数:8
--------------------------------
bool: 所占字节数:1 最大值:1 最小值:0
--------------------------------
char: 所占字节数:1 最大值: 最小值:€
signed char: 所占字节数:1 最大值: 最小值:€
unsigned char: 所占字节数:1 最大值: 最小值:
--------------------------------
short: 所占字节数:2 最大值:32767 最小值:-32768
unsigned short: 所占字节数:2 最大值:65535 最小值:0
wchar_t: 所占字节数:2 最大值:65535 最小值:0
--------------------------------
int: 所占字节数:4 最大值:2147483647 最小值:-2147483648
unsigned: 所占字节数:4 最大值:4294967295 最小值:0
--------------------------------
long: 所占字节数:4 最大值:2147483647 最小值:-2147483648
unsigned long: 所占字节数:4 最大值:4294967295 最小值:0
--------------------------------
double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308
long double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308
--------------------------------
float: 所占字节数:4 最大值:3.40282e+38 最小值:1.17549e-38
size_t: 所占字节数:8 最大值:18446744073709551615 最小值:0
string: 所占字节数:40
type: ************size**************
32位编译器结果(Visual Studio x86)
type: ************size**************
--------------------------------
*p: 所占字节数:4
--------------------------------
bool: 所占字节数:1 最大值:1 最小值:0
--------------------------------
char: 所占字节数:1 最大值: 最小值:€
signed char: 所占字节数:1 最大值: 最小值:€
unsigned char: 所占字节数:1 最大值: 最小值:
--------------------------------
short: 所占字节数:2 最大值:32767 最小值:-32768
unsigned short: 所占字节数:2 最大值:65535 最小值:0
wchar_t: 所占字节数:2 最大值:65535 最小值:0
--------------------------------
int: 所占字节数:4 最大值:2147483647 最小值:-2147483648
unsigned: 所占字节数:4 最大值:4294967295 最小值:0
--------------------------------
long: 所占字节数:4 最大值:2147483647 最小值:-2147483648
unsigned long: 所占字节数:4 最大值:4294967295 最小值:0
--------------------------------
double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308
long double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308
--------------------------------
float: 所占字节数:4 最大值:3.40282e+38 最小值:1.17549e-38
size_t: 所占字节数:4 最大值:4294967295 最小值:0
string: 所占字节数:28
type: ************size**************
参考:https://www.runoob.com/cplusplus/cpp-data-types.html
- 用 Pipeline 将训练集参数重复应用到测试集
- 什么是 ROC AUC
- SSE(Server-sent events)技术在web端消息推送和实时聊天中的使用
- 详解 Stacking 的 python 实现
- RESTful接口设计原则和优点
- 用 Doc2Vec 得到文档/段落/句子的向量表达
- 手把手用 IntelliJ IDEA 和 SBT 创建 scala 项目
- 项目中记录影响性能的缓慢数据库查询
- memory_profiler的使用
- 使用line_profiler查看api接口函数每行代码执行时间
- GAN 的 keras 实现
- 双向 LSTM
- scrapy回调函数传递参数
- python更新数据库脚本两种方法
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Java删除空字符:Java8 & Java11
- 6k+点赞的SpringBoot+Netty分布式即时通讯系统!爱了爱了!
- Day63:数据流中的中位数
- Prometheus是什么?
- Day64:滑动窗口的最大值
- 第10天:NLP补充——朴素贝叶斯(Naive-Bayes)
- Day65:矩阵中的路径
- Day66:机器人的运动范围
- 第11天:NLP补充——主题模式(LDA)
- 一文快速上手Rollup,JavaScript类库打包好帮手
- Day67:剪绳子
- Typecho实现微信发消息:Type时光机
- 插入排序
- Typecho1.1仿简书主题小屏幕菜单栏展开优化
- Typecho1.1仿简书主题大小屏适配