This is the mail archive of the gcc@gcc.gnu.org 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: C++ aliasing rules


mike stump <mrs@windriver.com> writes:

  > > To: Dan Nicolaescu <dann@godzilla.ICS.UCI.EDU>
  > > Cc: libstdc++@gcc.gnu.org, gcc@gcc.gnu.org
  > > From: Jason Merrill <jason@redhat.com>
  > > Date: Tue, 23 Apr 2002 10:43:27 +0100
  > 
  > > > struct first  {  int i; char a;  int f1;  char f2; double d;};
  > > > struct second {  char b;  int f2;  int f3;};
  > 
  > > > can it be assumed that given that "first" and "second" are
  > > > incompatible then ps1.f1 and ps2.f2 don't alias
  > 
  > This would be an excellent question for comp.std.c.  C++ mostly just
  > followed C on this. 

As a matter of fact I asked this question on comp.std.c sometime in
March. And the answer was that the C standard is a bit unclear in this
area...  

  >  We fixed a few things (made memcpy so that it can
  > be portably written within the rules of the language), but it should
  > mostly be the same.  You would need complete code, and might need to
  > `fix up' the example so that you can have them answer the question you
  > wanted.

A bit too much of my original post was snipped in the message you
replied to, that's why the code is incomplete, see:

http://gcc.gnu.org/ml/libstdc++/2002-04/msg00049.html


  > 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? 


  > No, before you implement this, you need to be aware that some
  > prefixing is probably ok:

I've already implemented it. 
I have an unsubmited patch that takes care of the prefixing thing
too. I'll reopen the discussion on the patches after 3.1 is released,
if we have an agreement that it's OK from the C++ standard point of
view. 

BTW, there's a defect report for C99 that says that allowing prefixing
is not such a great idea...

Thanks.
                --dan


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