C++复习笔记 常见数据类型占用的字节数

所占字节数即为sizeof()函数的输出值。

类型16位编译器32位编译器64位编译器备注
void000
bool111
char111
short222
int244
long448
float444
double888
enum244enum的变量其实质是整型
指针248指针存的是地址,所占字节数和编译器位数有关
classxxx与具体成员类型有关,最后需对齐
structxxx与具体成员类型有关,为所有成员的和,最后再对齐
unionxxx以最大存储量的成员为准,最后需对齐

查询代码

#include<iostream>
using namespace std;

int main()
{
    char str[] = "welcome to my blog";
    cout << "bool:\t " << sizeof(bool) << endl;
    cout << "char: \t" << sizeof(char) << endl;
    cout << "string: \t" << sizeof(str) << endl;
    cout << "short: " << sizeof(short) << endl;
    cout << "int: " << sizeof(int) << endl;
    cout << "long: " << sizeof(long) << endl;
    cout << "float: " << sizeof(float) << endl;
    cout << "double: " << sizeof(double) << endl;
    cout << "long double: " << sizeof(long double) << endl;
    cout << "bool*: " << sizeof(bool*) << endl;
    cout << "char*: " << sizeof(char*) << endl;
    cout << "short*: " << sizeof(short*) << endl;
    cout << "int*: " << sizeof(int*) << endl;
    cout << "long*: " << sizeof(long*) << endl;
    cout << "float*: " << sizeof(float*) << endl;
    cout << "double*: " << sizeof(double*) << endl;
    cout << "long double*: " << sizeof(long double*) << endl;
    enum color {red,green=0,blue,black,yellow,white};
    color color1;
    cout <<"枚举名称"<< sizeof(color)<<endl;
    cout << "枚举变量"<<sizeof(color1)<<endl;
    int array[5];
    cout << "数组" << sizeof(array)<<endl;
    class CGoods{
    public:
        //char name[33];
        int amount;
        //float totalvalue;
        //double price;
        //float a;
        //double b;
        void redister(char[], int, float);
        int getamount();
    };
    CGoods good1;     //所占总字节数 是 最大字节数的整数倍
    cout << "类名称" << sizeof(CGoods) << endl;
    cout << "类变量" << sizeof(good1) << endl;

    return 0;

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值