> > Dumb memcpy (while (len--) { *d++ = *s++ }) will have alignment problems> in any case. Intelligent ones, like the one provided in glibc, first copy> bytes till output is aligned (C file) *or* size is a multiple (i686 asm file)> of word size, and then it copies word-by-word.> > Linux's x86_64 memcpy does the opposite, copies 64bit words, and then> copies the last bytes.> > So, in effect, as long as no packed structures are used, memcpy should> be safer on *int, etc., than *char, as the compiler ensures> word-alignment.>

It most certainly does not. gcc will assume that an int* has int alignment. memcpy() is a builtin, which gcc can translate to pretty much anything. And C specifies that a pointer to foo, will point to a real object of type foo, so gcc can't be blamed for the unsafe typecasts. I have tested this the hard way, so this is not just speculation.

E.g., we have the following struct:

struct foo{ u8 a[4]; u32 b;};

This struct will have a size of 8 bytes and an alignment of 4 bytes (caused by the member b). Now take the following code:

void copy_foo(struct foo *dst, struct foo *src){ *dst = *src;}

On a platform that supports 64-bit loads and stores (e.g. AVR32, where I got hit by this), this will generate: