This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: memcpy() inlining and alignment
> struct s *src, dst;
>
> src = (struct s *)(cp + 1);
>
> memcpy(&dst, src, sizeof(struct s));
[...]
> 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.
I don't think so. In 7.12.2 of standard C, the description of memcpy
is
# The memcpy function copies n characters from the object pointed to
# by s2 into the object pointed to by s1. If copying takes place
# between objects that overlap, the behavior is undefined.
There is no mentioning of alignment constraints, and it says that it
is copying characters. So I think the compiler is wrong in assuming
that it can inline the memcpy using word-size instructions.
Regards,
Martin