This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: type based aliasing again -- a suggestion from a user


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.




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]