C++ 为什么如果 Unsigned Int >= 0 比较“毫无意义的比较”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5066362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:18:28  来源:igfitidea点击:

Why Is Comparing if an Unsigned Int >= 0 a "Pointless Comparison"?

c++cfor-loopcomparisonunsigned

提问by Josh

I got warning:

我收到警告:

Pe186 "Pointless comparison of unsigned int with zero"

Pe186“无符号整数与零的无意义比较”

when I tried to compile the following code:

当我尝试编译以下代码时:

for(clLoop = cpLoopStart; clLoop >= 0; clLoop--)                                  
{
    //Do something
}

I don't understand why. I could understand, if I were looking for a value less thanzero, since an unsigned intcan never be negative. But all I am looking for here is if it is equalto zero, which an unsigned intcertainly can be.

我不明白为什么。我可以理解,如果我正在寻找小于零的值,因为 anunsigned int永远不会是负数。但是,所有我在这里寻找是,如果它是等于为零,其中unsigned int当然也可以。

I could even see this error if in this loop I tried to pre-decrement instead of post-decrement, but again that is not the case.

如果在这个循环中我尝试预递减而不是递减后,我什至可以看到这个错误,但事实并非如此。

回答by Timbo

You check whether the unsigned int is greater than or equal (>=) zero. This expression will always be true, because unsigned integers will never be less than zero.

您检查 unsigned int 是否大于或等于 ( >=) 零。该表达式将始终为真,因为无符号整数永远不会小于零。

The compiler tries to warn you that you are about to program an infinite loop.

编译器试图警告您即将编写一个无限循环。

回答by Ole Begemann

You are checking if an unsigned int is equal or greater than0. Which is always true.

您正在检查 unsigned int 是否等于或大于0。这总是正确的。

回答by vtha

An unsigned integer never falls below 0 even after decrementing infinitely (i.e. clLoop >= 0will always be true), which makes the comparison pointless.

无符号整数即使在无限递减后也永远不会低于 0(即 clLoop >= 0永远为真),这使得比较毫无意义。

回答by Chris Becke

I think you meant to say

我想你是想说

for(clLoop = cpLoopStart; clLoop; clLoop--)                                  
{ //Do something
}

回答by Steve Jessop

clLoop >= 0is always true. It doesn't matter whether you pre-decrement or post-decrement, an unsigned value is at least 0. When you decrement 0you get UINT_MAX.

clLoop >= 0永远是真的。无论您是先减量还是后减量,无符号值至少为 0。当您递减时,0您会得到UINT_MAX.

The compiler figures that you probably don't meanto loop forever (or you'd have used a different construct, that more obviously loops forever), hence the warning.

编译器数字,你可能并不意味着要永远循环(或者你已经使用了不同的结构,更明显永远循环),因此警告。

回答by schnaader

The warning complains about your forloop break condition clLoop >= 0. The loop will end if clLoopgets negative, but that will never happen for an unsigned int.

该警告抱怨您的for循环中断条件clLoop >= 0。如果为clLoop负,循环将结束,但对于 unsigned int 将永远不会发生。

回答by Dennis V.R.

do{} while() can help you to use unsigned types of variable for loop without integer overflow:

do{} while() 可以帮助您使用无符号类型的变量 for 循环而不会出现整数溢出:

// main.c
#include <stdio.h>

int main(void)
{

    int array[] = {1,2,3,4,5,6,7,8,9,10};
    unsigned int size = sizeof(array)/sizeof(int); // size == 10;

    unsigned int i = size;
    do
    {
        i--;
        printf("Index: %u, content: %d\n",i,array[i]);

    } while(i > 0);

    return 0;
}

And compile it with:

并编译它:

gcc -std=c11 -Wall -Wextra -Wpedantic main.c

Output:

输出:

Index: 9, content: 10
Index: 8, content: 9
Index: 7, content: 8
Index: 6, content: 7
Index: 5, content: 6
Index: 4, content: 5
Index: 3, content: 4
Index: 2, content: 3
Index: 1, content: 2
Index: 0, content: 1

回答by Mayank Jindal

You should remove =in

您应该删除=

clLoop >= 0

Let's say your cpLoopStart is 5.

让我们说你的cpLoopStart is 5.

Then, clLoop value in the further iteratiosn would be -

然后,进一步迭代中的 clLoop 值将是 -

clLoop = 4;
clLoop = 3;
clLoop = 2;
clLoop = 1;
clLoop = 0;
clLoop = 0;
clLoop = 0;
clLoop = 0;
|
|
|
Infinite times.

回答by Nick Dong

gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11) in Centos 7 x86_64 does not give the warning.

Centos 7 x86_64 中的 gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11) 没有给出警告。

But when loop index is decremented to -1, loop index is implicitly converted to a value which is equal to UINT_MAX (limits.h)

但是当循环索引减少到 时-1,循环索引被隐式转换为一个等于 UINT_MAX ( limits.h) 的值

UINT_MAX + 1u is equal to 0. 0 - 1 is equal to UINX_MAX.

UINT_MAX + 1u is equal to 0. 0 - 1 is equal to UINX_MAX.

limits.h Various platform-dependent constants proposed by ANSI

limit.h ANSI 提出的各种平台相关常数

One of alternative solution is:

替代解决方案之一是:

unsigned int clLoop, i;

for(i = cpLoopStart+1, clLoop = i-1; i > 0; i--, clLoop = i-1)                                  
{
    //Do something
}

iwill change in the range [1, cpLoopStart+1]

i将在 [1, cpLoopStart+1] 范围内变化

clLoopwill change in the range [0, cpLoopStart]

clLoop将在 [0, cpLoopStart] 范围内改变