comparison is always false

J

John Ratliff

"comparison is always false due to limited range of data type"

I get this warning with g++ when I compile some code and I'm not quite
sure I understand it.

I have a small file that I've read into a memory buffer. It is defined
char sram[0x2000];

I need to check for a specific value within this buffer.

However, when I do

if (sram[36] == 0xC8) {
// blah blah
}

I get the warning.

What specifically is wrong with this? Is it a signedness issue? When I
use static_cast<unsigned char>(sram[36]) instead, I stop getting the
warning. Is this a correct solution, or am I simply silencing the error
without fixing the problem?

Thanks,

--John Ratliff
 
I

Ivan Novick

"comparison is always false due to limited range of data type"

I get this warning with g++ when I compile some code and I'm not quite
sure I understand it.

I have a small file that I've read into a memory buffer. It is defined
char sram[0x2000];

I need to check for a specific value within this buffer.

However, when I do

if (sram[36] == 0xC8) {
// blah blah

}

I get the warning.
I believe that ascii characters can be no bigger than 0x7F, so the
comparison will always be false because there are no characters 0xC8.
Are you putting ASCII data into the buffer or binary data?
 
K

Kai-Uwe Bux

John said:
"comparison is always false due to limited range of data type"

I get this warning with g++ when I compile some code and I'm not quite
sure I understand it.

I have a small file that I've read into a memory buffer. It is defined
char sram[0x2000];

I need to check for a specific value within this buffer.

However, when I do

if (sram[36] == 0xC8) {
// blah blah
}

I get the warning.

What specifically is wrong with this? Is it a signedness issue?

Most likely.
When I
use static_cast<unsigned char>(sram[36]) instead, I stop getting the
warning. Is this a correct solution, or am I simply silencing the error
without fixing the problem?

If you are willing to treat the buffer as a buffer of unsigned char, why not
declare

unsigned char sram[0x2000];

right away?


Best

Kai-Uwe Bux
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

"comparison is always false due to limited range of data type"

I get this warning with g++ when I compile some code and I'm not quite
sure I understand it.

I have a small file that I've read into a memory buffer. It is defined
char sram[0x2000];

I need to check for a specific value within this buffer.

However, when I do

if (sram[36] == 0xC8) {
// blah blah

}

I get the warning.

What specifically is wrong with this? Is it a signedness issue?

Yes, the char is (most probably) 8 bits signed which makes the value
range -128..127 IIRC. And 0xC8 == 200, which as you can see is not in
that range, thus false. When dealing with binary data you should
almost always use unsigned char.
 
J

John Ratliff

Yes, the char is (most probably) 8 bits signed which makes the value
range -128..127 IIRC. And 0xC8 == 200, which as you can see is not in
that range, thus false. When dealing with binary data you should
almost always use unsigned char.

That's what I thought, but I assumed the compiler would treat C8 as a
negative signed value, but perhaps it doesn't.

I could have used an unsigned char array, but it just requires casting
in different places. I thought using a signed char array would reduce
the number of explicit casts required. Binary file reading and some of
my string methods use signed chars.

Thanks,

--John Ratliff
 
R

Ron Natalie

John said:
That's what I thought, but I assumed the compiler would treat C8 as a
negative signed value, but perhaps it doesn't.

Nope, 0xC8 is a 200 decimal and is of type integer. When an int is
compared to a char, the char is converted to the int and not the
other way around (usual arithmatic conversions).


You could do
if (foo[32] == static_cast<char>(0xC8)) ...
 
H

Heinz Ozwirk

John Ratliff said:
That's what I thought, but I assumed the compiler would treat C8 as a
negative signed value, but perhaps it doesn't.

It doesn't. Octal and hexadecimal literals are usually treated as as the
first type of int, unsigned int, long or unsigned long, which is large
enough to hold them.
I could have used an unsigned char array, but it just requires casting in
different places. I thought using a signed char array would reduce the
number of explicit casts required. Binary file reading and some of my
string methods use signed chars.

If you want to compare a char variable with a constant value you should use
a char literal, '\xC8' in your case, or, even better, a named constant like
const char foo = '\xC8';.

HTH
Heinz
 
J

John Ratliff

Heinz said:
It doesn't. Octal and hexadecimal literals are usually treated as as the
first type of int, unsigned int, long or unsigned long, which is large
enough to hold them.


If you want to compare a char variable with a constant value you should use
a char literal, '\xC8' in your case, or, even better, a named constant like
const char foo = '\xC8';.

That's a perfect solution. Thanks.

So now I have

const char MAGIC_NUMBER = '\xC8';

if (sram[offset] == MAGIC_NUMBER) {
// ...
}

No more required cast, and no compiler warnings.

--John Ratliff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top