C Reference ยท string.h
C memcpy() function
Reference for C memcpy(): copy a block of bytes between non-overlapping objects, with array, struct and string examples and the rules that cause undefined behavior.
memcpy() copies a number of bytes from one memory block to another. It does not look at the contents: there is no terminator check and zero bytes are copied like any other. It is the fastest general-purpose way to copy arrays, structures and any raw data, and compilers often replace small fixed-size calls with a few move instructions.
Its one big restriction is that the source and destination must not overlap. If they might, use memmove.
#include <string.h> void *memcpy(void *restrict dest, const void *restrict src, size_t n);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
void *memcpy(void *restrict dest, const void *restrict src, size_t n);
| Name | Description |
|---|---|
dest |
Where to write. |
src |
Where to read from. |
n |
The number of bytes to copy. |
Returns: dest.
Errors / undefined behavior: undefined if the regions overlap, if either region is smaller than n bytes, or if either pointer is invalid. That includes NULL even when n is 0.
Usage notes
nis in bytes. For an array, usecount * sizeof elementorsizeof array. Passing the element count copies too little.- Do not use it when buffers can overlap. Shifting data within one array (inserting or deleting an element) needs
memmove. - Validate sizes before copying.
memcpy(dest, src, strlen(src) + 1)into a smaller buffer overflows. Compare againstsizeof destfirst. - No terminator, no type checks. It copies raw bytes. Copying a struct that holds pointers copies the pointers (a shallow copy), not what they point to. Structs also copy their padding bytes.
- Use it for type punning. Copying bytes between objects of different types (as in the
floatexample) is well defined, unlike casting a pointer and reading through it, which breaks the strict aliasing rule. Compilers turn the copy into a register move. - Non-trivial data. Do not
memcpyobjects that own resources (an openFILE, a mutex, a struct with a self-pointer) and then use both copies. - Performance: typically memory-bandwidth bound; libraries use vectorized code. Calling it with small constant sizes is as cheap as a plain assignment.
- Zero-length copies are fine as long as the pointers are valid.
NULLsource withn == 0is formally undefined; guard against it if your code can reach that case.
Related
memmove,memset,memcmp,strcpyandstrncpy,restrict- Lessons: Strings, Void pointers