This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: type based aliasing again
- To: Alexandre Oliva <oliva AT lsd.ic.unicamp.br>
- Subject: Re: type based aliasing again
- From: Bernd Schmidt <bernds AT cygnus.co.uk>
- Date: Tue, 14 Sep 1999 13:34:22 +0100 (BST)
- cc: Joe Buck <jbuck AT synopsys.COM>, gcc AT gcc.gnu dot org, rms AT gnu dot org
> BTW, it seems to me that this `cast to union' is something particular
> to gcc, not a general solution, right? I mean, other compilers might
> not pay as much attention to the unions as gcc does, since accessing
> any type other than the one that was actually stored in the union is
> undefined behavior anyway.
If, by "cast to union", you mean code of the form
union foo
{
double a;
int b[2];
};
double x;
x = 2.0;
((union foo *)&x)->b[1] = 0;
then this is not even going to work with gcc, regardless of whether
-fstrict-aliasing is on or not. We found code like this in some math
library code here at Cygnus a few weeks ago. The problem is that gcc's
MEM_IN_STRUCT/MEM_SCALAR_P alias analysis will catch cases like this and
determine that the integer memory access can't alias the variable x.
The code should be written
union foo
{
double a;
int b[2];
};
union foo x;
x.a = 2.0;
x.b[1] = 0;
The guarantee that this will work is a gcc extension.
Bernd