1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\" and Copyright 2015 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" References consulted:
7 .\" Linux libc source code
8 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
10 .\" Modified Sun Jul 25 10:41:09 1993 by Rik Faith (faith@cs.unc.edu)
11 .TH MEMCPY 3 2021-03-22 "Linux man-pages (unreleased)"
13 memcpy \- copy memory area
16 .RI ( libc ", " \-lc )
19 .B #include <string.h>
21 .BI "void *memcpy(void *restrict " dest ", const void *restrict " src \
27 function copies \fIn\fP bytes from memory area
28 \fIsrc\fP to memory area \fIdest\fP.
29 The memory areas must not overlap.
32 if the memory areas do overlap.
36 function returns a pointer to \fIdest\fP.
38 For an explanation of the terms used in this section, see
46 Interface Attribute Value
49 T} Thread safety MT-Safe
55 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
57 Failure to observe the requirement that the memory areas
58 do not overlap has been the source of significant bugs.
59 (POSIX and the C standards are explicit that employing
61 with overlapping areas produces undefined behavior.)
62 Most notably, in glibc 2.13
63 .\" glibc commit 6fb8cbcb58a29fff73eb2101b34caa19a7f88eba
64 a performance optimization of
66 on some platforms (including x86-64) included changing the order
67 .\" From forward copying to backward copying
68 in which bytes were copied from
73 This change revealed breakages in a number of applications that performed
74 copying with overlapping areas.
75 .\" Adobe Flash player was the highest profile example:
76 .\" https://bugzilla.redhat.com/show_bug.cgi?id=638477
77 .\" Reported: 2010-09-29 02:35 EDT by JCHuynh
78 .\" Bug 638477 - Strange sound on mp3 flash website
80 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=12518
81 .\" Bug 12518 - memcpy acts randomly (and differently) with overlapping areas
82 .\" Reported: 2011-02-25 02:26 UTC by Linus Torvalds
84 Under the previous implementation,
85 the order in which the bytes were copied had fortuitously hidden the bug,
86 which was revealed when the copying order was reversed.
88 .\" glibc commit 0354e355014b7bfda32622e0255399d859862fcd
89 a versioned symbol was added so that old binaries
90 (i.e., those linked against glibc versions earlier than 2.14)
93 implementation that safely handles the overlapping buffers case
94 (by providing an "older"
96 implementation that was aliased to