memcpy() inlining and alignment

David S. Miller
Fri Mar 3 17:33:00 GMT 2000

   From: Jason Thorpe <>
   Date: Fri, 03 Mar 2000 17:22:06 -0800

   I'm not sure I buy that.  What you're effectively saying is:

	   There is no standard function in C that allows unaligned
	   data to be copied into an aligned buffer.

   Personally, I have never encountered a libc implementation of memcpy()
   that required data to be aligned in the way you're suggesting.

libc does not have available the type and alignment information
that the compiler does.

For example:

struct foo {
       unsigned int x;

int read_foo(struct foo *p)

Can the compiler assume that loading the p->x value via a
32-bit word load instruction is safe?  If so, why can it not
make the same assumption when inlining a call to memcpy.
How do the alignment rules for such a structure change in
the two cases?

Libc could in fact perform the same optimization, by inlining
the implementation in the strings.h header file by way of the
__align_of__ etc. compiler builtins.  In fact, if I am not
mistaken, glibc does do similar to this.

I'm not saying that any of this is legal, what I am saying is
what the compiler is doing and based upon what is making these

David S. Miller

More information about the Gcc-bugs mailing list