[PATCH]: Fix PR tree-optimization/21407
Daniel Berlin
dberlin@dberlin.org
Tue May 10 18:13:00 GMT 2005
> Construct 2:
>
> struct Base
> {
> int a;
> int b;
> };
>
> struct Derived
> {
> int c;
> int d;
> struct Base b;
> int e;
> int f;
> };
>
> void foo(struct Base *b)
> {
> struct Derived *d =
> (struct Derived *) ((char *)b) - offsetof(struct Derived, b);
>
> d->c = 23;
> }
>
> void bar(struct Derived *d)
> {
> foo (&d->b);
> }
> However, it is my opinion that construct 2 *is* allowed by the
> standard. Obviously nothing fishy is happening up till the first
> statement of foo. foo begins by converting b to a char*, which is an
> escape hatch - everything aliases an array of char (6.5p7), and the
> pointer is guaranteed to point to the first byte of Derived.b
> (6.3.2.3p7).
If i change "struct Base b" to "int b", and have foo take an "int *b" is
it still legal?
After all, you are still casting to a char * what appears to be a
pointer guaranteed to point to the beginning of Derived, by the same
logic.
I only ask this question because it has optimization implications. if
it's only legal to do this when you pass a pointer to a structure that
is a member, and not a pointer to any old type member, i can simply
modify the code to take this into account.
More information about the Gcc-patches
mailing list