First let me start off by saying that I know char
, signed char
and unsigned char
are different types in C++. From a quick reading of the standard, it also appears that whether char
is signed
is implementation-defined. And to make things just a little more fun, it appears g++
decides whether a char
is signed
on a per-platform basis!
So anyway with that background, let me introduce a bug I've run into using this toy program:
#include <stdio.h>
int main(int argc, char* argv[])
{
char array[512];
int i;
char* aptr = array + 256;
for(i=0; i != 512; i++) {
array[i] = 0;
}
aptr[0] = 0xFF;
aptr[-1] = -1;
aptr[0xFF] = 1;
printf("%d\n", aptr[aptr[0]]);
printf("%d\n", aptr[(unsigned char)aptr[0]]);
return 0;
}
The intended-behavior is that both calls to printf
should output 1. Of course, what happens on gcc
and g++ 4.6.3
running on linux/x86_64
is that the first printf
outputs -1 while the second outputs 1. This is consistent with chars being signed and g++
interpreting the negative array index of -1 (which is technically undefined behavior) sensibly.
The bug seems easy enough to fix, I just need to cast the char
to unsigned
like shown above. What I want to know is whether this code was ever expected to work correctly on an x86 or x86_64 machines using gcc/g++
? It appears this may work as intended on ARM platform where apparently chars are unsigned, but I would like know whether this code has always been buggy on x86 machines using g++
?