cast lvalues optimizing and strict-aliasing

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

from my understanding of the type-punning aliasing rule, the following code is correct, and indeed compiles fine with gcc/trunk :

typedef struct {
  short a : 16;
  short b : 16;
} c;

foo(c t1)
{
  char *pt1 = (char*)&t1;

  if (*(int*)(pt1))
    return 0;

  return 1;
}

However if I try to rewrite it using an intermediate cast :

foo(c t1)
{
  if (*((int*)((char*)&t1)))
    return 0;

  return 1;
}

the compiler emits the "dereferencing type-punned..." warning message.

Indeed the gimple tree doesn't show the (char *) cast:

;; Function foo (null)
;; enabled by -tree-original


  if (*(int *) &t1 != 0)
    {
      return 0;
    }
  return 1;
}

And the alias analyser see the types as not-compatible. So was the parser allowed to ignore the (char *) cast ? I think this would change the aliasing semantic, but before suspecting a bug, I would very much like to have other opinions.

many thanks

Christian



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux