This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: type based aliasing again -- a suggestion from a user
- To: gcc at gcc dot gnu dot org
- Subject: Re: type based aliasing again -- a suggestion from a user
- From: "Marcel Cox" <Marcel dot Cox at cie dot etat dot lu>
- Date: Tue, 14 Sep 1999 11:44:20 +0200
- CC: george at moberg dot com, mark at codesourcery dot com
- Organization: CIE
- Reply-To: marcel dot cox at cie dot etat dot lu
Here is a small sample program that actually breaks with -o2 or higher:
======
#include <stdio.h>
double transform(double x)
{
/* calculates (x+1)/2 */
/* the division by 2 is done in a dirty way */
/* this code is specific for ix86 processors */
x+=1.0;
((int *)&x)[1]-=0x100000;
return x;
}
int main()
{
double x;
x=2.0;
printf("%f %f\n",x,transform(x));
x=-2.0;
printf("%f %f\n",x,transform(x));
return 0;
}
=========
The alias rule violation is of course very clear in this example with
the invalid typecast from double* to int*.
However I wonder if most of the alias problems are not of similar
nature, and if a warning for invalid or suspicious pointer typecasts
might not help many people sotting the problem.