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
Daniel Berlin wrote:
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.
Yes, in my mind, that's the whole point of offsetof.
Do you think it's legal if the fields are in the other order:
Yes, but only because of rules about initial members, etc.
It's certainly possible that I'm wrong. Joseph is the person I trust
most to know the C standard, and I'll be interested in his opinion.
But, casting pointers to aribtrary types is specifically allowed by the
standard -- so long as you cast them back before you use them. This
program:
struct S {
int i;
double d;
};
void f(int * p) {
struct S* s = (struct S*) p;
s->d = 3.0;
}
int main () {
struct S s;
f(&s.i);
}
is definitely valid. I don't think this has anything at all to do with
common initial prefix rules; the only important thing is that "s"
actually point to an object of type "struct S", which it does. The
initial prefix rules are about accessing a "struct Derived" using a
"struct Base *", or vice versa; they give you an allowance to do that,
even though you can't normally use a "struct Foo*" to access a "struct Bar".
--
Mark Mitchell
CodeSourcery, LLC
mark@codesourcery.com
(916) 791-8304