场景
- 自从有了
C++11的auto关键字之后,声明变量经常使用它。之后有一次发现编译控制台大量输出有符号/无符号不匹配的警告信息。怎么回事?
说明
-
这里的有符号和无符号指的就是整数的类型,无符号的整数无论怎么运算都不会出现负数。 -
计算机里存储无符号的类型是通过原码,也就是二进制格式存储。而有符号的正数是通过原码存储,负数是通过补码存储的。 -
问题就是出现了如下的一种错误, 在使用比较运算符 < 是操作数的类型不匹配,一个是有符号,一个是无符号。可能导致错误的比较结果。因为有无符号的等级更高些,有符号的操作数会被转换为无符号再进行比较。这里的警告就是提醒如果有负值的时候比较的结果可能就是错误的。
testcpp.cpp(55): warning C4018: “<”: 有符号/无符号不匹配
C的sizeof()函数返回的就是unsigned int类型,而这个函数经常用在对数组求大小时。当我们使用auto声明变量size的时候,默认就是sizeof的类型了。
int ns[] = { 4,5,6,7,78,8 };
auto size = sizeof(ns) / sizeof(int);
- 这时候如果使用
size在循环里做减法希望得到负数的时候就会出问题,因为unsigned int类型不会有负数。比如对数组倒序求解, i-0 时候等于 4294967295,这个循环就会进入死循环。
for (auto i = size - 1; i >= 0; --i) {
cout << i << endl;
assert(i < size);
}
方案
- 最好的方法就是在求解
size的时候不要使用auto,使用int类型让它隐式转换。
int size = sizeof(ns) / sizeof(int);
- 其他使用
unsigned int类型的时候,如果和常量整数(默认是int有符号)或变量进行运算时,都要转换为统一的类型再运算。
例子
#include <string.h>
#include <iostream>
#include <bitset>
#include <assert.h>
using namespace std;
void TestUnsignedInt() {
unsigned char a = 1;
unsigned char b = 3;
unsigned char res = a - b;
unsigned char ma = 255;
cout << (unsigned)ma << endl;
cout << (unsigned)res << endl;
cout << bitset<8>(res) << endl;
}
void TestUnsignedSizeofFunc() {
int ns[] = { 4,5,6,7,78,8 };
auto size = sizeof(ns) / sizeof(int);
cout << "++++++" << endl;
for (auto i = 0; i < size; i++)
cout << i << endl;
cout << "------" << endl;
for (auto i = size - 1; i >= 0; --i) {
cout << i << endl;
assert(i < size);
}
}
int main()
{
TestUnsignedInt();
TestUnsignedSizeofFunc();
}
|