ARM memcpy on unaligned pointers and -O2
Richard Earnshaw
rearnsha@arm.com
Thu Dec 13 11:05:00 GMT 2001
> On Thu, Dec 13, 2001 at 09:53:01AM -0700, Shaun Jackman wrote:
>
> > I recognized that afterwords and cast the arguments to memcpy()
> >
> > uint32
> > ld32( const uint32* p)
> > {
> > uint32 ret;
> > memcpy( &ret, (uint8*)p, sizeof( ret));
> > return ret;
> > }
> >
> > void
> > str32( uint32* p, uint32 val)
> > {
> > memcpy( (uint8*)p, &val, sizeof( *p));
> > }
> >
> > It seems the behaviour persists though. In fact, it has the exact same
> > behaviour if I change the parameters from uint32* to void*.
>
> This behavior matches my experience with GCC, as well. Other compilers
> (such as Digital Unix's) to honor the casts in this case, but for GCC,
> you have to actually do this:
>
> char *cp = (char *) src;
> memcpy(&ret, cp, sizeof(ret));
>
> I think GCC should honor the cast, like other compilers do.
I don't think even this can be expected to work. From the moment you put
something that isn't a uint32* into p, you've lied to the compiler. If a
uint32 * is an aligned type, then it must only contain aligned pointers;
playing games with assignments like this is just trying to cover up your
lie -- but eventually the compiler will find you out.
R.
More information about the Gcc-bugs
mailing list