This is the mail archive of the gcc-bugs@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]

[Bug c++/56394] pointer arithmetic breaks with __attribute__((packed))


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56394

--- Comment #6 from Maxim Yegorushkin <maxim.yegorushkin at gmail dot com> 2013-02-19 14:31:10 UTC ---
(In reply to comment #5)
> (In reply to comment #4)
> > (In reply to comment #2)
> > > (In reply to comment #1)
> > > > Also reproduces with optimization but with -fno-inline.  The issue must be
> > > > with the exact division:
> > > > 
> > > >   D.2616_5 = end.0_3 - beg.1_4;
> > > >   D.2617_6 = D.2616_5 /[ex] 12;
> > > > 
> > > > the difference is 4088 but that does not divide by 12.  Which means your
> > > > computed end is not correct (the pointers cannot point to elements of
> > > > an array of Xyz).
> > > 
> > > That is, you probably want
> > > 
> > >     template<class T>
> > >         T* end() {
> > >             auto e = reinterpret_cast<uintptr_t>(static_cast<char*>(mem) +
> > > len);
> > >             return reinterpret_cast<T*>(e - len % sizeof(T));
> > >         }
> > > 
> > > instead.
> > 
> > Is there anything wrong with my original code?
> 
> Yes, pointer subtraction returns an index into an array, but your pointers
> do not point to array elements but elsewhere.  e % sizeof(T) depends on
> the alignment of e, not on the size of the array.

You are right Richard, it should not take into consideration the base address
of
the mapping in end<>.

Thanks a lot.


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