B
Boon
Hello everyone,
I've run across the following (IMHO incorrect) code.
const char TAB[96];
typedef unsigned char uint8;
static void foo(char *buf)
{
uint8 i=0;
while ((i < MAX_DIGITS) && (buf != 0))
{
if ((uint8)buf >= 0xA0)
buf = TAB[buf - 0xa0];
i++;
}
}
I have a problem with the index computation.
(Please correct me wherever I am wrong.)
NB : on my platform, plain char is signed, char is 8-bits wide.
Suppose buf = 0xCE, then (uint8)buf is indeed >= 0xA0
What is the type and value of the expression buf - 0xa0 ?
It might depend on my platform's representation of signed numbers?
(sign-and-magnitude, 1's complement, 2's complement)
http://en.wikipedia.org/wiki/Signed_number_representations
Assuming 2's complement, 0xCE represents -50
I think buf is promoted to an int
0xa0 is of type int (??) and has value 160
Thus the expression has type int and value -210, right?
Thus the code, in fact, performs TAB[-210] which means we've erred
into UB-land, right?
Thanks for any clarification.
Regards.
I've run across the following (IMHO incorrect) code.
const char TAB[96];
typedef unsigned char uint8;
static void foo(char *buf)
{
uint8 i=0;
while ((i < MAX_DIGITS) && (buf != 0))
{
if ((uint8)buf >= 0xA0)
buf = TAB[buf - 0xa0];
i++;
}
}
I have a problem with the index computation.
(Please correct me wherever I am wrong.)
NB : on my platform, plain char is signed, char is 8-bits wide.
Suppose buf = 0xCE, then (uint8)buf is indeed >= 0xA0
What is the type and value of the expression buf - 0xa0 ?
It might depend on my platform's representation of signed numbers?
(sign-and-magnitude, 1's complement, 2's complement)
http://en.wikipedia.org/wiki/Signed_number_representations
Assuming 2's complement, 0xCE represents -50
I think buf is promoted to an int
0xa0 is of type int (??) and has value 160
Thus the expression has type int and value -210, right?
Thus the code, in fact, performs TAB[-210] which means we've erred
into UB-land, right?
Thanks for any clarification.
Regards.