This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: memcpy() inlining and alignment
- To: thorpej at netbsd dot org
- Subject: Re: memcpy() inlining and alignment
- From: "David S. Miller" <davem at redhat dot com>
- Date: Fri, 3 Mar 2000 16:25:35 -0800
- CC: gcc-bugs at gcc dot gnu dot org
- References: <20000304002452.7907.qmail@mail.netbsd.org>
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