This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH]: Fix PR tree-optimization/21407
On Tue, 2005-05-10 at 09:30 -0700, Mark Mitchell wrote:
> Daniel Berlin wrote:
>
> > IE it's not legal in C to do something like this:
> >
> > struct foobase
> > {
> > int b;
> > };
> > struct foo
> > {
> > int a;
> > struct foobase b;
> > };
> >
> > void func (struct foobase *a)
> > {
> > struct foo *b = (struct foo *)(((char *)a) - (offsetof (b, b) -
> > offsetof (b, a))
> >
> > /* Yay, i've got a struct foo * back! */
> > if (b->a != 5)
> > abort();
> > }
> >
> > int main(void)
> > {
> > struct foo a;
> > a.a = 5;
> > func(&a.b);
> > return 0;
> > }
>
> Why is that not legal? The only reason I can imagine is that you've
> done pointer arithmetic that goes beyond a single array,
What makes you think it is legal?
> but if there's
> no exemption for character pointers in that case, there should be, at
> least in GNU C, where we assume unsegmented memory.
If i give you a pointer to a field, that would mean you could add or
subtract bytes and legally get at any other member of the structure,
containing structure, etc.
I was told by Geoff Keating, and others, that this is not legal in C.
Have I been misinformed? That would be quite sad.
Kenny's fields analysis also depends on this behavior being illegal in C
(we make specific provisions for clobbering up and down the inheritance
hierarchy for languages like C++, C).
In fact, almost all useful type based and interprocedural alias analysis
i can think of depends on this not being legal without some more
explicit relation (IE base/derived, etc) between the two structures.
>
> Do you think it's legal if the fields are in the other order:
Yes, but only because of rules about initial members, etc.