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: memcpy to an unaligned address


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


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