unsigned / double propagation weirdness?

Richard Guenther rguenth@tat.physik.uni-tuebingen.de
Mon Feb 23 16:36:00 GMT 2004


Falk Hueffner wrote:
> 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.

Warning about such would be indeed very useful.  We even warn about signed/unsigned
comparisons, so there is precedent.

Richard.



More information about the Gcc mailing list