zoukankan      html  css  js  c++  java
  • 从C++ int类型的变量范围谈起

    从学习C语言开始,int类型所占字节数,以及数值范围就是一个挥之不去的问题。一开始会死记硬背一个char 1个字节,一个字节8个bit。64位机器上面一个int 4个字节,32位机器上面不一样。那时候并不知道编译器也分很多种,每一种实现的细节不一样,也不知道各家编译器是遵循C++标准委员会的标准。后来学会像编译器求证,以其输出为准,也学会向标准求证。

    在一篇博客上面看到用numeric_limits 类可以方便地了解各个类型的取值范围以及占用内存,于是满心欢喜地将代码运行,发现有些问题:

    #include<iostream>
    #include <limits>
    using namespace std;
    
    int main()
    {
        cout << "type: \t\t" << "------------------size-----------------------"<< endl;
        cout << "bool: \t\t" << "Bytes:" << sizeof(bool);
        cout << "\tMaxValue:" << (numeric_limits<bool>::max)();
        cout << "\t\tMinValue:" << (numeric_limits<bool>::min)() << endl;
        cout << "char: \t\t" << "Bytes:" << sizeof(char);
        cout << "\tMaxValue:" << (numeric_limits<char>::max)();
        cout << "\t\tMinValue:" << (numeric_limits<char>::min)() << endl;
        cout << "signed char: \t" << "Bytes:" << sizeof(signed char);
        cout << "\tMaxValue:" << (numeric_limits<signed char>::max)();
        cout << "\t\tMinValue:" << (numeric_limits<signed char>::min)() << endl;
        cout << "unsigned char: \t" << "Bytes:" << sizeof(unsigned char);
        cout << "\tMaxValue:" << (numeric_limits<unsigned char>::max)();
        cout << "\t\tMinValue:" << (numeric_limits<unsigned char>::min)() << endl;
        cout << "wchar_t: \t" << "Bytes:" << sizeof(wchar_t);
        cout << "\tMaxValue:" << (numeric_limits<wchar_t>::max)();
        cout << "\t\tMinValue:" << (numeric_limits<wchar_t>::min)() << endl;
        cout << "short: \t\t" << "Bytes:" << sizeof(short);
        cout << "\tMaxValue:" << (numeric_limits<short>::max)();
        cout << "\t\tMinValue:" << (numeric_limits<short>::min)() << endl;
        cout << "int: \t\t" << "Bytes:" << sizeof(int);
        cout << "\tMaxValue:" << (numeric_limits<int>::max)();
        cout << "\tMinValue:" << (numeric_limits<int>::min)() << endl;
        cout << "unsigned: \t" << "Bytes:" << sizeof(unsigned);
        cout << "\tMaxValue:" << (numeric_limits<unsigned>::max)();
        cout << "\tMinValue:" << (numeric_limits<unsigned>::min)() << endl;
        cout << "long: \t\t" << "Bytes:" << sizeof(long);
        cout << "\tMaxValue:" << (numeric_limits<long>::max)();
        cout << "\tMinValue:" << (numeric_limits<long>::min)() << endl;
        cout << "type: \t\t" << "************size**************"<< endl;
        return 0;
    }

    运行结果:

     

    常用的int,long等类型的取值范围确实如愿显示。但是,在关于 char的判断中,有显示的问题。从使用的角度而言,其实char用于表示字符,判断大小并不合理。当然,这只是一个“完形填空”式的推导。既然显示在屏幕上的输出有问题,本着钻研精神,要弄清楚为什么。

    首先是假设:

    1. cout无法处理这个numeric_limit<char>::max()函数

    2.numeric_limit<char>::max()的返回值并不是可显示字符。

    为了验证第一点,我找到C++标准委员会的网站,找到C++14的草稿标准:

    从这里看出,返回的是模板类型 T的构造函数。似乎排除了第一种可能。但标准太不具体,我在Clion上面用debug模式直接找到库文件来确认。值得一提的是编译器版本:gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.2)。

    第一步:

    再往下一层:

    在limits的实现里面,都是再用宏定义的位操作来实现最大和最小值的获取。先看__glibcxx_digits宏定义。这里面的逻辑很简单,占用字节数乘以__CHAR_BIT__减去符号位(1位)。在char的例子里面,得出来的位数是7。从这里不妨猜测,编译器对一个字节大小的定义很有可能是基于__CHAR_BIT__的。在一篇博文看过对于古老的机器,char类型只有7bit。按照这个猜测,在描述内置int型大小的时候,就可以说一个int等于4个char。这个猜测挺有意思,不过有待考究。

     进一步分析,__glicxx_min宏定义,对于传入的类型_Tp,如果有符号,就执行(_Tp)1 << _glibcxx_digits(_Tp), 没有就是(_Tp)0.

    对于char型,它居然被是认为有符号的! 所以最小值的计算变成 (char)1 << 7,二进制表示10000000, -128.

    一个被赋值为-128的char类型字符,其实现的动机难以理解,不过从某程度上说明标准与实现之间的距离,对于一个函数的实现,尤其是不常用的功能,最好还是自己进行确认。

  • 相关阅读:
    福建工程学院第十四届ACM校赛B题题解
    2018 ACM-ICPC青岛现场赛 B题 Kawa Exam 题解 ZOJ 4059
    联合周赛第二场 我在哪?题解
    维修数列 Splay(这可能是我写过最麻烦的题之一了。。。用平衡树维护dp。。。丧心病狂啊。。。。)
    虚树入门!世界树!
    御坂御坂题解(出自北航校赛) 约瑟夫环问题高效解决方案
    网络流24题! 开始!题解!
    AFO
    【模板库】减维的模板库【停更】
    【组合数学】Educational Codeforces Round 83 (Rated for Div. 2) D题
  • 原文地址:https://www.cnblogs.com/kinsang/p/6696216.html
Copyright © 2011-2022 走看看