This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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;

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
(916) 791-8304

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]