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]
Other format: [Raw text]

ISO Aliasing rules question


Given this testcase:

struct s1 { double d; };
struct s2 { double d; };

double f(struct s1 *a, struct s2 *b)
{
  a->d = 1.0;
  return b->d + 1.0;
}

int main()
{
  struct s1 a;
  a.d = 0.0;
  if (f (&a, (struct s2 *)&a) != 2.0)
    abort ();
  return 0;
}


Does this code produce undefined behavior according to ISO standard,
particularly in regards to aliasing issues.

My recollection of the standard is that struct s1 and struct s2 are 
_NOT_ type compatible (even though they have identical internal structure).

Thus the variables a & b in function f can't refer to the same object
as they are not type compatible.  Thus the compiler should be free to
ignore the assignment a->d = 1.0 when evaluating return b->d + 1.0
(which is precisely what the tree-ssa branch does :-)

Is this correct?  Or is my recollection of the ISO standard incorrect here?

Jeff



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