Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

Warning #186: pointless comparison of unsigned integer with zero

SergeyKostrov
Valued Contributor II
4,826 Views

Here are two cases when a Warning #186 is displayed:

// Case 1:

...
for( RTuint t = 0; t < ( ( RTuint )NUMBER_OF_TESTS - 1 ); t++ )
{
...
}
...

../.cpp(nnnn): warning #186: pointless comparison of unsigned integer with zero

Note 1: 'RTuint t = 0' is not a comparison with 0. It is assignment of 0 to the variable 't'.

// Case 2:

...
if( pIoParams->uiEOLBytes >= 0 && pIoParams->uiEOLBytes <= 2 )
{
...
}
else
break;
...

../.cpp(nnnn): warning #186: pointless comparison of unsigned integer with zero

Note 2: The 'if' statement verifies that 'pIoParams->uiEOLBytes' is in a range from 0 to 2 and
valid values are as follows: 0, 1, or 2.
A processing is not allowed if the value of 'pIoParams->uiEOLBytes' is outside of the range.

A word 'pointless' seems to me too "harsh" because I really know what I need to verify and a more neutral message
will look significantly better, like:

Warning #186: possibly unnecessary comparison of unsigned integer with zero

and please consider this.

My Development Environment:

OS: Windows XP 32-bit
IDE: Visual Studio 2005 SP1
Compilers: Intel C++ / Microsoft C++ / Borland C++ / MinGW / Turbo C++

Intel C++ compiler version ( Composer XE 2011 Update 9 ):

Intel C++ Compiler XE 12.1.3.300 [IA-32]

Intel C++ compiler command line options:

/c /Od /D "WIN32" /D "_CONSOLE" /D "_DEBUG" /D "_VC80_UPGRADE=0x0710"
/D "_UNICODE" /D "UNICODE" /GF /EHsc /RTC1 /MTd /GS /fp:precise /W5 /nologo /ZI /TP
/Qopenmp /Qdiag-disable:111,673

Best regards,
Sergey

0 Kudos
5 Replies
Judith_W_Intel
Employee
4,826 Views

In case 1 why do you think the compiler is complaining about theassignment and not the conditional?

In case 2:

if( pIoParams->uiEOLBytes >= 0 && pIoParams->uiEOLBytes <= 2 )

What is the point of the first conditional? if uiEOLBytes is unsigned then it is always going to be
greater than or equal to 0.

Why didn't you just write the if statementlike:

if( pIoParams->uiEOLBytes <= 2 )

Judy
0 Kudos
SergeyKostrov
Valued Contributor II
4,826 Views

In case 1 why do you think the compiler is complaining about theassignment and not the conditional?

...
for( RTuint t = 0; t < ( ( RTuint )NUMBER_OF_TESTS - 1 ); t++ )
^
../.cpp(nnnn): warning #186: pointless comparison of unsigned integer with zero


[SergeyK] When a message is displayed there is a circumflex sign pointing to 't = 0' part of the 'for' statement.

In case 2:

if( pIoParams->uiEOLBytes >= 0 && pIoParams->uiEOLBytes <= 2 )

What is the point of the first conditional? if uiEOLBytes is unsigned then it is always going to be
greater than or equal to 0.

Why didn't you just write the if statementlike:

if( pIoParams->uiEOLBytes <= 2 )

[SergeyK] I agree, but this is implicit form.Due tostrict coding standards on the projectexplicit forms must
be always used.


I simply would like to repeat that a more neutral message will look better:

Warning #186: possibly unnecessary comparison of unsigned integer with zero

Best regards,
Sergey

0 Kudos
Brandon_H_Intel
Employee
4,826 Views

Sergey,

Are you sure you're seeing the carat appearing in the right place? Here's my code which I based on yours:

[cpp]#include const int NUMBER_OF_TESTS=1; void goo(unsigned int n); void foo() { for(unsigned int t = 0; t < ((unsigned int) NUMBER_OF_TESTS-1); t++) goo(t); }[/cpp]

And my output:

[bash]Q:u105822>icl /Wall /c test2.cpp Intel C++ Intel 64 Compiler XE for applications running on Intel 64, Ve rsion 12.1.4.325 Build 20120410 Copyright (C) 1985-2012 Intel Corporation. All rights reserved. test2.cpp test2.cpp(8): warning #186: pointless comparison of unsigned integer with zero for(unsigned int t = 0; t < ((unsigned int) NUMBER_OF_TESTS-1); t++) ^



[/bash]For some reason the code syntax is changing the less than symbol to html lt, but the carat for me is pointing to the comparison.
0 Kudos
Judith_W_Intel
Employee
4,826 Views

Yes I see the same thing as Brandon as far as where the carot is pointing.

And I don't think the "sometimes" is accurate -- it's always unnecessary to compare an unsigned number to 0. So I don't really seea problem with the text of the diagnostic.

Judy
0 Kudos
SergeyKostrov
Valued Contributor II
4,826 Views
...Are you sure you're seeing the carat appearing in the right place?... Here's my code which I based on yours:

  1. #include
  2. constintNUMBER_OF_TESTS=1;
  3. voidgoo(unsignedintn);
  4. voidfoo(){
  5. for(unsignedintt=0;t<((unsignedint)NUMBER_OF_TESTS-1);t++)
  6. goo(t);
  7. }
...


No. Could youdo a very quick verification with:

...
const int NUMBER_OF_TESTS = 1024;
...

and please set a 'Tahoma' font (size 8 ) for a Visual Studio's Output Window.

Best regards,
Sergey

0 Kudos
Reply