简体   繁体   English

如果出现这种情况,为什么会收到编译器警告?

[英]Why am I getting a compiler warning for this if condition?

if(nowPlayingIndex-1 >= 0){ }

I am using this condition in a function and I am getting the following compiler warning in xCode: 我在函数中使用此条件,并且在xCode中收到以下编译器警告:

Comparison of unsigned expression >= 0 is always true. 无符号表达式> = 0的比较始终为true。

How can this be true? 这怎么可能是真的? If the value of nowPlayingIndex is <= 0 then the above condition is false. 如果nowPlayingIndex的值nowPlayingIndex <= 0则上述条件为假。

Many thanks. 非常感谢。

I'm sure the type of nowPlayingIndex is unsigned integral type. 我确定nowPlayingIndex的类型是无符号整数类型。 If so, then nowPlayingIndex -1 will be unsigned integral type also, which can never be negative, hence nowPlayingIndex -1 is always greater than or equal to 0 . 如果是这样,那么nowPlayingIndex -1也将是无符号整数类型,该类型永远不能为负,因此nowPlayingIndex -1始终大于或等于0

Therefore, you should write : 因此,您应该写:

if ( nowPlayingIndex  >= 1 ) 

nowPlayingIndex is apparently unsigned, so nowPlayingIndex-1 can never be negative. nowPlayingIndex显然是无符号的,因此nowPlayingIndex-1永远不能为负。 Therefore the condition is always true, as the compiler is warning you. 因此,条件始终为true,因为编译器会警告您。

nowPlayinfIndex seems to be unsigned. nowPlayinfIndex似乎是未签名的。 This means it's always positive. 这意味着它总是积极的。 If you go negative, you'll have a buffer overflow (underflow?) 如果为负,则将有缓冲区溢出(下溢?)

Looking at how these things work in binary...Take an 8-bit signed integer for example: 看一下这些东西在二进制中是如何工作的...以一个8位带符号整数为例:

10000000 (-127)
10000001 (-126)

with unsigned: 带有未签名:

10000000 (127)
10000001 (128)

EDIT: Fixed the numbers 编辑:固定数字

The left most bit, or the sign bit determines if your number is + or -. 最左边的位或符号位确定您的电话号码是+还是-。 When it's 1, you can consider it to be -127, so when you add it to your running total, you get a negative number. 当它为1时,您可以认为它是-127,因此,将其添加到运行总计中时,将得到一个负数。 However, with an 8-bit UNSIGNED integer, the sign bit has a value of +127. 但是,对于8位UNSIGNED整数,符号位的值为+127。 This is also why signed integers cannot store as large + numbers as unsigned. 这也是为什么有符号整数不能存储与无符号一样大的数字。

If nowplayingindex is unsigned, it cannot be < 0 . 如果nowplayingindex是无符号的,则不能< 0 And if I subtract 1, unsignedness wins and I get the maximum value possible for this datatype, still being a positive number. 如果我减去1,则无符号会获胜,并且我会获得此数据类型可能的最大值,但仍为正数。

You must work with signed values or cast to such one. 您必须使用带符号的值或将其强制转换为此类值。

You did not mention what is the datatype of nowPlayingIndex see my comment. 您没有提到nowPlayingIndex的数据类型是nowPlayingIndex请参阅我的评论。 If it is unsigned int then that is your problem. 如果它是unsigned int ,那是您的问题。

Just for fun, try to subtract 1 from an unsigned integer that is initilized with the value 0. Check your result. 只是为了好玩,尝试从初始化为0的无符号整数中减去1。检查结果。

That is a bit weird, but try the following and see if it helps. 有点奇怪,但是请尝试以下操作,看看是否有帮助。

if ((nowPlayingIndex - 1) >= 0)
{
    //logic
}

Expanding on the earlier correct answers, if nowPlayingIndex == 0 , then nowPlayingIndex - 1 is the maximum value of its type, eg, UINT_MAX, which might be 4294967295. 扩展先前的正确答案,如果nowPlayingIndex == 0 ,则nowPlayingIndex - 1是其类型的最大值,例如UINT_MAX,可能为4294967295。

You probably want to write: 您可能想写:

if (nowPlayingIndex > 0) { ... }

or 要么

if (nowPlayingIndex >= 1 { ... }

if nowPlayingIndex is unsigned char 如果nowPlayingIndex是unsigned char

unsigned char nowPlayingIndex = 0;
nowPlayingIndex = nowPlayingIndex - 1; // 255 (UCHAR_MAX in limits.h)

It means exactly what it says on the tin. 它的意思恰好是它在罐子上所说的。 nowPlayingIndex is unsigned, nowPlayingIndex - 1 is unsigned as well, and all unsigned values are >= 0. nowPlayingIndex是无符号的,nowPlayingIndex-1也同样是无符号的,并且所有未签名的值> = 0。

Try to use signed values instead. 尝试改用带符号的值。 Ever since I rewrote all of my stuff to use signed values (my own int32 more precisely), everything is simpler and correctness is easier to ensure. 自从我重写了所有内容以使用带符号的值(更准确地说是我自己的int32)以来,一切都变得更加简单,而且更容易确保正确性。

If that is too much effort, or not possible, simply use nowPlayingIndex >= 1. 如果这太费力或不可能,只需使用nowPlayingIndex> = 1。

You are wrong, 0 - 1 can be greater than 0. I will show you: 您错了, 0 - 1可以大于0。我将向您展示:

$ cat foo.cpp

#include <iostream>

int main()
{
    unsigned int foo = 0;
    ::std::cout << (foo - 1) << "\n";
    return 0;
}

$ g++ -O3 foo.cpp -o foo

$ ./foo
4294967295

There you go. 妳去

变量nowPlayingIndex可能是一个无符号整数 ,代表一个正数 ,它始终> = 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM