From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2099 invoked by alias); 25 Apr 2002 17:45:30 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 1369 invoked from network); 25 Apr 2002 17:44:06 -0000 Received: from unknown (HELO executor.cambridge.redhat.com) (195.224.55.237) by sources.redhat.com with SMTP; 25 Apr 2002 17:44:06 -0000 Received: from prospero.cambridge.redhat.com (dell-paw-2.cambridge.redhat.com [195.224.55.226]) by executor.cambridge.redhat.com (Postfix) with ESMTP id 1BAE7ABAF8; Thu, 25 Apr 2002 18:44:06 +0100 (BST) Received: by prospero.cambridge.redhat.com (Postfix, from userid 4046) id 77880F7B7B; Thu, 25 Apr 2002 18:44:05 +0100 (BST) To: Dan Nicolaescu Cc: mike stump , gcc@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: Re: C++ aliasing rules References: <200204240235.TAA23236@kankakee.wrs.com> <200204241327.aa13343@gremlin-relay.ics.uci.edu> From: Jason Merrill In-Reply-To: <200204241327.aa13343@gremlin-relay.ics.uci.edu> (Dan Nicolaescu's message of "Wed, 24 Apr 2002 13:27:06 -0700") Date: Thu, 25 Apr 2002 10:57:00 -0000 Message-ID: User-Agent: Gnus/5.090004 (Oort Gnus v0.04) Emacs/21.1 (i686-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-SW-Source: 2002-04/txt/msg01338.txt.bz2 >>>>> "Dan" == Dan Nicolaescu writes: > mike stump writes: >> Consider: >> >> struct { char buf[sizeof (double)]; short i; } secondo, *second = &secondo; >> struct { double other; short i; } firsto, *not_really_first = &secondo; >> >> and the question of whether or not first->i can ever alias second->i. >> >> I suspect the closest we can come would be to >> >> memcpy (&secondo, &firsto, sizeof (secondo)); >> >> and then play with not_really_first->i and second->i. >> >> I think the answer is no, as no matter what you do, you need to >> violate the notion of which struct type the object really was and you >> cannot get at the short without going though the struct first, and in >> the end, you cannot get through both struct types simultaneously. > I like this answer. :-) > Does anybody disagree with Mike's statement above (wrt C++) ? Jason? I don't see how Mike's statement answers my question. Given struct A { char buf[sizeof (double)]; short i; }; struct B { double other; short i; }; A a; A* ap = &a; B* bp = reinterpret_cast(ap); referring to bp->i is accessing a.i through an lvalue of a class type (B) which contains a member of a.i's type (short). The standard seems to say that's OK. Again, if instead we consider a.i to be an indivisible part of 'a' for purposes of [basic.lval], then the access would be undefined. I would probably support writing that into the standard. But I don't think that's what it says now. Jason