| 1 |
'\" t |
| 2 |
.\" Copyright 1993, David Metcalfe <[email protected]> |
| 3 |
.\" Copyright 2015, Michael Kerrisk <[email protected]> |
| 4 |
.\" Copyright, the authors of the Linux man-pages project |
| 5 |
.\" |
| 6 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 7 |
.\" |
| 8 |
.TH memcpy 3 2026-02-08 "Linux man-pages 6.18" |
| 9 |
.SH NAME |
| 10 |
memcpy \- copy memory area |
| 11 |
.SH LIBRARY |
| 12 |
Standard C library |
| 13 |
.RI ( libc ,\~ \-lc ) |
| 14 |
.SH SYNOPSIS |
| 15 |
.nf |
| 16 |
.B #include <string.h> |
| 17 |
.P |
| 18 |
.BR "void *memcpy(" "size_t n;" |
| 19 |
.BI " void " dest "[restrict " n "], const void " src "[restrict " n ], |
| 20 |
.BI " size_t " n ); |
| 21 |
.fi |
| 22 |
.SH DESCRIPTION |
| 23 |
The |
| 24 |
.BR memcpy () |
| 25 |
function copies |
| 26 |
.I n |
| 27 |
bytes from memory area |
| 28 |
.I src |
| 29 |
to memory area |
| 30 |
.IR dest . |
| 31 |
The memory areas must not overlap. |
| 32 |
Use |
| 33 |
.BR memmove (3) |
| 34 |
if the memory areas do overlap. |
| 35 |
.SH RETURN VALUE |
| 36 |
The |
| 37 |
.BR memcpy () |
| 38 |
function returns a pointer to |
| 39 |
.IR dest . |
| 40 |
.SH ATTRIBUTES |
| 41 |
For an explanation of the terms used in this section, see |
| 42 |
.BR attributes (7). |
| 43 |
.TS |
| 44 |
allbox; |
| 45 |
lbx lb lb |
| 46 |
l l l. |
| 47 |
Interface Attribute Value |
| 48 |
T{ |
| 49 |
.na |
| 50 |
.nh |
| 51 |
.BR memcpy () |
| 52 |
T} Thread safety MT-Safe |
| 53 |
.TE |
| 54 |
.SH STANDARDS |
| 55 |
C11, POSIX.1-2008. |
| 56 |
.SH HISTORY |
| 57 |
POSIX.1-2001, C89, SVr4, 4.3BSD. |
| 58 |
.SH CAVEATS |
| 59 |
Failure to observe the requirement that the memory areas |
| 60 |
do not overlap has been the source of significant bugs. |
| 61 |
(POSIX and the C standards are explicit that employing |
| 62 |
.BR memcpy () |
| 63 |
with overlapping areas produces undefined behavior.) |
| 64 |
Most notably, in glibc 2.13 |
| 65 |
.\" glibc commit 6fb8cbcb58a29fff73eb2101b34caa19a7f88eba |
| 66 |
a performance optimization of |
| 67 |
.BR memcpy () |
| 68 |
on some platforms (including x86-64) included changing the order |
| 69 |
.\" From forward copying to backward copying |
| 70 |
in which bytes were copied from |
| 71 |
.I src |
| 72 |
to |
| 73 |
.IR dest . |
| 74 |
.P |
| 75 |
This change revealed breakages in a number of applications that performed |
| 76 |
copying with overlapping areas. |
| 77 |
.\" Adobe Flash player was the highest profile example: |
| 78 |
.\" https://bugzilla.redhat.com/show_bug.cgi?id=638477 |
| 79 |
.\" Reported: 2010-09-29 02:35 EDT by JCHuynh |
| 80 |
.\" Bug 638477 - Strange sound on mp3 flash website |
| 81 |
.\" |
| 82 |
.\" https://sourceware.org/bugzilla/show_bug.cgi?id=12518 |
| 83 |
.\" Bug 12518 - memcpy acts randomly (and differently) with overlapping areas |
| 84 |
.\" Reported: 2011-02-25 02:26 UTC by Linus Torvalds |
| 85 |
.\" |
| 86 |
Under the previous implementation, |
| 87 |
the order in which the bytes were copied had fortuitously hidden the bug, |
| 88 |
which was revealed when the copying order was reversed. |
| 89 |
In glibc 2.14, |
| 90 |
.\" glibc commit 0354e355014b7bfda32622e0255399d859862fcd |
| 91 |
a versioned symbol was added so that old binaries |
| 92 |
(i.e., those linked against glibc versions earlier than 2.14) |
| 93 |
employed a |
| 94 |
.BR memcpy () |
| 95 |
implementation that safely handles the overlapping buffers case |
| 96 |
(by providing an "older" |
| 97 |
.BR memcpy () |
| 98 |
implementation that was aliased to |
| 99 |
.BR memmove (3)). |
| 100 |
.SH SEE ALSO |
| 101 |
.BR bcopy (3), |
| 102 |
.BR bstring (3), |
| 103 |
.BR memccpy (3), |
| 104 |
.BR memmove (3), |
| 105 |
.BR mempcpy (3), |
| 106 |
.BR strcpy (3), |
| 107 |
.BR strncpy (3), |
| 108 |
.BR wmemcpy (3) |