The situation:
I have a piece of code that works when compiled for 32-bits but fails when compiled for 64-bits with gcc 4.6. After identifying the problem and reading up on the standards, I cannot really understand why it is working for 32-bits. I hope someone can explain what is going on.
The code (somewhat simplified and cut down to the interesting parts):
// tbl: unsigned short *, can be indexed with positive and negative values
// v: unsigned int
// p: unsigned char *
tmp = tbl[(v >> 8) - p[0]]; // Gives segfault when not compiled with -m32
When compiled with -m32
the code works. When compiled without -m32
it gives a segfault. The reason for the segfault is that (v >> 8) - p[0]
is interpreted as an unsigned int
when compiled for 64-bit which for "negative" results will be way off.
According to this question, the C99 standard says the following:
6.2.5c9: A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.
From this it seems that unsigned
minus unsigned
will always result in unsigned
output which is consistent with what happens in the 64-bit case. This does not seem to happen in the 32-bit case which is what I find extremely strange.
Can anyone explain what is happening in the 32-bit case?