This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: memcpy to an unaligned address
- From: Falk Hueffner <falk at debian dot org>
- To: Shaun Jackman <sjackman at gmail dot com>
- Cc: gcc at sources dot redhat dot com
- Date: Tue, 02 Aug 2005 19:47:53 +0200
- Subject: Re: memcpy to an unaligned address
- References: <7f45d9390508021032aea5a61@mail.gmail.com>
Shaun Jackman <sjackman@gmail.com> writes:
> In a typical Ethernet/IP ARP header the source IP address is
> unaligned. Instead of using...
> out->srcIPAddr = in->dstIPAddr;
> ... I used...
> memcpy(&out->srcIPAddr, &in->dstIPAddr, sizeof(uint32_t));
> ... to account for the unaligned destination. This worked until gcc 4,
> which now generates a simple load/store.
> ldr r3, [r6, #24]
> adds r2, r4, #0
> adds r2, #14
> str r3, [r2, #0]
> A nice optimisation, but in this case it's incorrect. $r4 is aligned,
> and the result of adding #14 to $r4 is an unaligned pointer.
It isn't incorrect; gcc can assume that pointers are always correctly
aligned for their type. Anything else would result in horrible code.
If your program forms a pointer that is not properly aligned, it is
already invalid, and later breakage is only a symptom of that.
--
Falk