Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Does simple inheritance break strict-aliasing rule Date: Wed, 21 Mar 2018 10:20:13 -0700 Organization: None to speak of Lines: 44 Message-ID: References: <87h8p9tofh.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="8fef62c5babeabfae505a3b4aebc529d"; logging-data="23638"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180O0noFlKFJsYLbpgxNFkN" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:qyHz4seLvbnGy7205Qf/cBwpOzY= sha1:/DRQpNnLZwpGaoPB/PR2rtlnRik= Xref: csiph.com comp.lang.c:128159 supercat@casperkitty.com writes: > On Wednesday, March 21, 2018 at 10:54:06 AM UTC-5, Ben Bacarisse wrote: >> C has no "strict-aliasing rule". It has what are called effective type >> rules and these say that you can't access an object of one type though >> an lvalue expression of another type. That's a gross simplification of >> course, but none of the details are in play here. Provided s->type is >> never equal RECT when s is not, in fact, pointing at the Shape inside a >> Rect, then you are not breaking C's rules when you write r->whatever. > > A literal reading of the rules would indicate that given: > > struct foo {int x; } *p; > > the lvalue "p->x" has type "int". As such, it may be used to access any > object whose type is simply "int", but may not be used to access any object > of type "struct foo". Of course p->x is of type int. > The rules would make a lot of sense if "p->x" were regarded for purposes of > 6.5p7 as an lvalue of type "struct foo" No, it would make no sense at all to treat p->x as an lvalue of type struct foo. Would you want this to be legal? struct foo {int x; } *p; struct foo obj; // assume reasonable initializations p->x = obj; If p->x is regarded as an lvalue of type struct foo, why would that last assignment not be legal? *p is of type struct foo because p is defined as an object of type struct foo*. p->x is of type int because x is defined as a member of type int. [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"