mount_setattr.2: Minor tweaks to Christian's patch
[man-pages.git] / man3 / memcpy.3
bloba9ab476b0f7dade532facb70050b679d3a7ad77d
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\" and Copyright 2015 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" References consulted:
27 .\"     Linux libc source code
28 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
29 .\"     386BSD man pages
30 .\" Modified Sun Jul 25 10:41:09 1993 by Rik Faith (faith@cs.unc.edu)
31 .TH MEMCPY 3  2021-03-22 "" "Linux Programmer's Manual"
32 .SH NAME
33 memcpy \- copy memory area
34 .SH SYNOPSIS
35 .nf
36 .B #include <string.h>
37 .PP
38 .BI "void *memcpy(void *restrict " dest ", const void *restrict " src \
39 ", size_t " n );
40 .fi
41 .SH DESCRIPTION
42 The
43 .BR memcpy ()
44 function copies \fIn\fP bytes from memory area
45 \fIsrc\fP to memory area \fIdest\fP.
46 The memory areas must not overlap.
47 Use
48 .BR memmove (3)
49 if the memory areas do overlap.
50 .SH RETURN VALUE
51 The
52 .BR memcpy ()
53 function returns a pointer to \fIdest\fP.
54 .SH ATTRIBUTES
55 For an explanation of the terms used in this section, see
56 .BR attributes (7).
57 .ad l
58 .nh
59 .TS
60 allbox;
61 lbx lb lb
62 l l l.
63 Interface       Attribute       Value
65 .BR memcpy ()
66 T}      Thread safety   MT-Safe
67 .TE
68 .hy
69 .ad
70 .sp 1
71 .SH CONFORMING TO
72 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
73 .SH NOTES
74 Failure to observe the requirement that the memory areas
75 do not overlap has been the source of significant bugs.
76 (POSIX and the C standards are explicit that employing
77 .BR memcpy ()
78 with overlapping areas produces undefined behavior.)
79 Most notably, in glibc 2.13
80 .\" glibc commit 6fb8cbcb58a29fff73eb2101b34caa19a7f88eba
81 a performance optimization of
82 .BR memcpy ()
83 on some platforms (including x86-64) included changing the order
84 .\" From forward copying to backward copying
85 in which bytes were copied from
86 .I src
88 .IR dest .
89 .PP
90 This change revealed breakages in a number of applications that performed
91 copying with overlapping areas.
92 .\" Adobe Flash player was the highest profile example:
93 .\"   https://bugzilla.redhat.com/show_bug.cgi?id=638477
94 .\"   Reported: 2010-09-29 02:35 EDT by JCHuynh
95 .\"   Bug 638477 - Strange sound on mp3 flash website
96 .\"
97 .\"   https://sourceware.org/bugzilla/show_bug.cgi?id=12518
98 .\"   Bug 12518 - memcpy acts randomly (and differently) with overlapping areas
99 .\"   Reported:       2011-02-25 02:26 UTC by Linus Torvalds
101 Under the previous implementation,
102 the order in which the bytes were copied had fortuitously hidden the bug,
103 which was revealed when the copying order was reversed.
104 In glibc 2.14,
105 .\" glibc commit 0354e355014b7bfda32622e0255399d859862fcd
106 a versioned symbol was added so that old binaries
107 (i.e., those linked against glibc versions earlier than 2.14)
108 employed a
109 .BR memcpy ()
110 implementation that safely handles the overlapping buffers case
111 (by providing an "older"
112 .BR memcpy ()
113 implementation that was aliased to
114 .BR memmove (3)).
115 .SH SEE ALSO
116 .BR bcopy (3),
117 .BR bstring (3),
118 .BR memccpy (3),
119 .BR memmove (3),
120 .BR mempcpy (3),
121 .BR strcpy (3),
122 .BR strncpy (3),
123 .BR wmemcpy (3)