This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: memcpy() inlining and alignment


   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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]