

Memmove vs. Memcpy in C: Safety, Performance, and Best Uses
Copying bytes is simple—until the source and destination overlap. In C, memcpy and memmove both copy a specified number of bytes, but they do not offer the same guarantees. Knowing when to use each function helps prevent subtle bugs without giving up performance unnecessarily.
How Memcpy and Memmove Differ
Both functions are declared in <string.h> and return the destination pointer. They copy n bytes; neither function adds a string terminator or understands the data’s type. The caller must ensure that the source and destination each refer to valid regions of at least n bytes.
Memcpy requires non-overlapping regions
Use memcpy only when the source and destination ranges do not overlap. If they do, the C standard defines the behavior as undefined. The result may appear correct in one build and fail after a compiler or library change, so overlap is not a case to rely on.
Memmove supports overlap
memmove guarantees the result as if the source bytes were first copied into a temporary, non-overlapping array and then into the destination. That describes the required behavior, not a requirement that the implementation allocate a temporary buffer. Libraries commonly choose a safe copy direction or use other optimized techniques.
For overlapping ranges, the implementation must preserve source bytes that have not yet been copied. It can copy in an appropriate direction, but the exact strategy is an implementation detail. Do not assume that a particular address comparison or copying loop is portable across all objects and platforms.
Practical C Examples
Use memmove when shifting data within the same buffer. This example removes the first two bytes from a character array while keeping the remaining bytes and terminator:
#include <stdio.h>
#include <string.h>
int main(void) {
char text[] = "abcdef";
memmove(text, text + 2, strlen(text + 2) + 1);
puts(text); // cdef
return 0;
}
The source starts inside the destination array, so the ranges overlap. The length includes the null terminator because this example moves a C string, not just its visible characters.
For a non-overlapping copy, memcpy is appropriate:
char source[] = "hello";
char destination[sizeof source];
memcpy(destination, source, sizeof source);
Here, the arrays are separate and the destination is large enough. If overlap is possible or difficult to rule out, use memmove instead.
Choosing Between Memmove and Memcpy
- Choose
memcpywhen you can establish that the regions do not overlap. - Choose
memmovewhen regions overlap, or when overlap is possible and correctness matters more than relying on a non-overlap assumption. - For either function, verify the byte count and the bounds of both regions.
These functions operate on bytes. They do not check buffer sizes, validate pointers, or prevent out-of-bounds access. A correct choice of function cannot compensate for an invalid pointer or an incorrect length.
Memmove vs. Memcpy Performance
It is common to describe memmove as slower because it must handle overlap. That is not a universal rule. Actual performance depends on the compiler, C library, processor, copy size, alignment, and whether the ranges overlap. Optimized libraries may use specialized routines, and compilers may replace calls with inline code when they can determine the size or conditions.
When source and destination are known to be separate, memcpy communicates that non-overlap condition and may give the compiler more optimization freedom. But for a small copy, the difference may be negligible; for large copies, memory bandwidth and hardware behavior often matter more than the function name. Benchmark representative workloads on the target system rather than assuming one function is always faster.
Never use memcpy on overlapping regions as a performance shortcut. Its undefined behavior makes the code incorrect, regardless of how fast a particular test appears. For timing-sensitive systems, assess both worst-case execution time and correctness under the actual compiler and target; see memmove in Real-Time Applications.
Common Uses for Memmove
memmove is useful when bytes need to shift within a buffer—for example, removing a section, inserting data after making room, or compacting remaining content. It is also relevant in performance-critical code that manipulates packed data, where the measured workload and overlap pattern determine the best approach. For more on that context, read memmove in High-Performance Computing.
Shift bytes to make room
To insert bytes into a buffer, first ensure there is enough capacity, then move the existing tail toward the end before writing the new bytes. The move may overlap, so memmove is the appropriate function:
memmove(buffer + position + count,
buffer + position,
used - position);
This snippet assumes buffer has enough capacity for the additional count bytes and that position and used are valid. Those checks belong in the surrounding code.
Portability and Correctness
The overlap guarantee is part of memmove’s standard behavior, but details such as internal copy direction, vectorization, and speed vary among implementations. Write code to the function’s contract rather than depending on a particular library’s internals. See Cross-Platform Behavior of memmove for more on portability.
As with other byte-copying functions, use care with object representations and bounds. Copying bytes is not a substitute for correctly managing object lifetimes or types in C++; in either language, the function does not interpret the data being moved.
Conclusion
The practical distinction is straightforward: memcpy is for non-overlapping regions, while memmove is safe when regions overlap. Their real-world speed depends on the workload and implementation, so measure where performance matters—but never trade away defined behavior for a benchmark result. Check the byte count and buffer bounds, choose the function whose contract matches your data, and let correctness guide optimization.