memcpy to an unaligned address

Shaun Jackman sjackman@gmail.com
Tue Aug 2 22:26:00 GMT 2005


On 8/2/05, Paul Koning <pkoning@equallogic.com> wrote:
> It sounds like the workaround is to avoid memcpy, and just use
> variable assignment.  Alternatively, cast the pointers to char*, which
> should force memcpy to do the right thing.  Ugh.

Casting to void* does not work either. gcc keeps the alignment
information -- but not the *unalignment* information, if that
distinction makes any sense -- of a particular variable around as long
as it can, through casts and even through assignment. The unalignment
information, on the other hand, is lost immediately after the &
operator. None of these examples produce an unaligned load:

	memcpy(&s->b, &n, sizeof n);

	memcpy((void*)&s->b, &n, sizeof n);

	void *p = &s->b;
	memcpy(p, &n, sizeof n);

But as pointed out by others, this does produce an unaligned load:

	s->b = n;

Cheers,
Shaun



More information about the Gcc mailing list