This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: module level flags
Bruce Korb <bkorb@pacbell.net> writes:
> Zack Weinberg wrote:
>
> > I'm afraid the compiler is working as designed.
> >
> > > YYSTYPE def,
> > > YYSTYPE list )
> > > {
> > > + tDefEntry* ret = (tDefEntry*)list;
> > > tDefEntry* pDef = (tDefEntry*)def;
> > > - tDefEntry* pScn = (tDefEntry*)list;
> > > - tDefEntry** ppT = (tDefEntry**)&list;
> > > + tDefEntry* pScn = ret;
> > > + tDefEntry** ppT = &ret;
> >
> > In the original code, the type-based aliasing rules permit the
> > compiler to assume that assignment to ppT does not modify list
> > EVEN THOUGH IT IS OBVIOUS TO A HUMAN THAT IT DOES.
> >
> > Your fix is the right way to correct your code. By immediately
> > assigning list to a temporary with its true type, you show the
> > compiler what's really going on.
>
> That is completely goofy. Is that really the intent of the
> official standard, or GCC's interpretation?
Yes, that's the intent of the official standard.
There's no guarantee that two pointers to different types have the
same representation. For instance, if you have
tDefEntry foo;
void *x;
*(tDefEntry **)&x = &foo;
if (x != (void *)&foo)
abort();
this could call abort, or do anything else (since it invokes undefined
behavior).
In fact, there's no guarantee even that 'sizeof (void *)' is the same
as 'sizeof (tDefEntry *)'.
--
- Geoffrey Keating <geoffk@geoffk.org>