[Bug c/69776] Wrong optimization with aliasing

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Feb 12 04:02:00 GMT 2016


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69776

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
I'm not sure if the question of dynamic memory and pointers at function scope
as applies to the test case is settled or not, or that there is consensus about
the validity of the submitted test case.  My impression is that the weakest
(mostly, within WG14) agreed-upon requirement on implementations is the visible
union rule (N1520).  Under it, I would expect the following slightly changed
test case to produce the expected output (1 1).  It doesn't with GCC and I
would be more inclined to view that as a bug than the original test case (GCC
isn't the only compiler that fails this test).  The next weakest requirement is
that the effective type of an object cannot change after it's set.  I suspect
that's too weak to be useful in general.

#include <stdlib.h>
#include <stdio.h>

int main()
{
  void *p = malloc(10);

  union {
    int *pi;
    double *pd;
  } u = { .pi = p };

  *u.pi = 1;

  printf("*pi = %d\n", *u.pi);

  int a = *u.pi;

  *u.pd = 0;

  *u.pi = a;

  printf("p = %p\n", p);
  printf("*pi = %d\n", *u.pi);
}


More information about the Gcc-bugs mailing list