memcpy() inlining and alignment
David S. Miller
davem@redhat.com
Fri Mar 3 16:30:00 GMT 2000
From: Jason Thorpe <thorpej@netbsd.org>
Date: Fri, 03 Mar 2000 16:24:52 -0800
struct s {
unsigned int a;
unsigned int b;
unsigned int i;
};
...
struct s *src, dst;
src = (struct s *)(cp + 1);
memcpy(&dst, src, sizeof(struct s));
...
However, when this code is compiled with -O1 or -O2, the memcpy() calls
are inlined by the compiler. The result is unaligned access in the first
case and correct access in the second case.
I believe the compiler is correctly assuming that the structure is
aligned to whatever alignment is required by the members of the
structure type for a particular processor's ABI. In this case, most
likely the required alignment is 4 bytes on 32-bit systems.
If this is the case, you'll need to create your own
"memcpy_unaligned()" or similar interface for these specific
cases.
Later,
David S. Miller
davem@redhat.com
More information about the Gcc-bugs
mailing list