Skip to content

memcpy vs memmove

MarekBykowski edited this page May 26, 2026 · 1 revision
void *memcpy (void *dst, const void *src, size_t n);  // UB if regions overlap
void *memmove(void *dst, const void *src, size_t n);  // safe if regions overlap
memcpy  — assumes NO overlap, may use SIMD/vectorised copy → faster
memmove — handles overlap: copies via temp buffer or adjusts direction
char buf[] = "hello world";

// Safe — regions don't overlap
memcpy(buf + 6, "there", 5);    // "hello there"

// UNSAFE with memcpy — overlapping regions
memcpy(buf + 1, buf, 5);        // UB! use memmove instead
memmove(buf + 1, buf, 5);       // correct

// Classic use: shifting array elements
memmove(&a[1], &a[0], n * sizeof(int));  // insert at front

💡 Rule of thumb: if in doubt, use memmove. The performance difference is negligible unless you're in a hot loop.

Clone this wiki locally