Richard Guenther <rguenth@tat.physik.uni-tuebingen.de> writes:
Hi, I'm getting confusing (but consistent along a lot of different
compilers) results out of this testcase:
int main(int argc, char **argv)
{
double dm = 1.0;
unsigned int i = 1;
if (-i*dm != -((double)i)*dm)
abort();
return 0;
}
From the standard I cannot understand why this should abort.
The point is that "-i" is still an unsigned number. "-i" basically
means "~i + 1", so it's something largish, but not someting negative.
Another cute instance is:
int a[10];
int *b = a+1;
unsigned i = 1;
return b[-i];
This works on 32 bit platforms and crashes on 64 bit platforms.
A while ago I proposed generally warning about negation of unsigned
entities, but there were a few objections, and I never got around to
finishing it.