6 ms·
I was skeptical about his example of a compiler bug. Take the following similar example: int bar(void) { unsigned x = 1; int y = -1;
by dadkins 17y ago
I was skeptical about his example of a compiler bug. Take the following similar example:
int bar(void) {
unsigned x = 1;
int y = -1;
return x > y;
}
Surprisingly, the result is always 0, at least on every version of gcc I've tried. How could this be? It turns out unsigned's are one area where the C standard really screwed up. When comparing a signed and an unsigned value, the compiler helpfully promotes the signed (-1) to unsigned.
I was surprised the given example with chars instead of ints didn't give the same result. Was it really a bug, or just ambiguity in the standard?
- fhub 17y agoshort behaves the same as char. int, long and long long all implicitly cast. Very odd. gcc v4.4.1, x86
- ori_b 17y ago> When comparing a signed and an unsigned value, the compiler helpfully promotes the signed (-1) to unsigned. So what do you think the compiler should do? Convert the unsigned into a signed? Disallow comparison/assignment/addition/... without an explicit cast?
- regehr 17y agoIt was really a bug, and they fixed it: https://bugs.launchpad.net/ubuntu/hardy/+source/gcc-4.2/+bug/256797 https://bugs.launchpad.net/ubuntu/hardy/+source/gcc-4.2/+bug...