The memmove() function from <string.h> copies n bytes from source to destination—just like memcpy()—but it is safe when the two regions overlap. That makes it the right choice for shifting text inside one buffer or any in-place memory rearrangement.
01
Copy n Bytes
Like memcpy.
02
Overlap Safe
Key difference.
03
Returns dest
Same pointer.
04
Shift In Place
Same buffer.
05
+1 for Strings
Include \0.
06
vs memcpy()
When to pick each.
Fundamentals
Definition and Usage
In C, memmove() is declared in <string.h>. It transfers n bytes from src to dest and returns dest. When the ranges overlap, the implementation copies in the correct direction so source bytes are not overwritten before they are read.
Use memmove() whenever source and destination might be the same array or share memory—for example, removing the first character from a string or inserting bytes in the middle of a buffer. When regions are guaranteed disjoint, memcpy() is also valid and may be marginally faster.
💡
Beginner Tip
When in doubt, choose memmove(). It behaves like memcpy() for non-overlapping copies but stays defined when overlap occurs—a common situation in string editing and buffer compaction.
Destination starts at a higher address than source, so memmove copies backward internally to avoid overwriting unread bytes. After the shift, we write 'H' at index 0.
Example 4 — Remove a Character From the Middle
Close a gap after deleting one character from a string.
C
#include <stdio.h>
#include <string.h>
int main(void) {
char word[] = "Hello";
size_t removeAt = 1; /* remove 'e' at index 1 */
memmove(word + removeAt, word + removeAt + 1,
strlen(word) - removeAt);
printf("After removal: %s\n", word);
return 0;
}
📤 Output:
After removal: Hllo
How It Works
Bytes from index 2 onward shift left to index 1, including the trailing '\0'. This is a typical text-editor “delete character” pattern built on overlap-safe copying.
Example 5 — When memcpy() Is Enough
Use memcpy only when regions are guaranteed not to overlap.
memcpy is fine for independent buffers. For in-buffer edits like buffer + 2 → buffer, always use memmove. The shifted result is "erlap" (first two characters overwritten by the shift).
Applications
🚀 Common Use Cases
In-place string edits — delete or insert characters without a second buffer.
Buffer compaction — shift live data to the start of an array after removing headers.
Safe generic copy — use when overlap cannot be ruled out in library code.
Ring buffers — rearrange bytes when linearizing circular queue contents.
Defensive coding — prefer memmove when pointer arithmetic makes overlap possible.
🧠 How memmove() Works
1
You pass dest, src, and n
Same parameters as memcpy—destination, source, byte count.
Input
2
Check for overlap
If ranges overlap, copy backward or forward so source bytes are preserved until read.
Safety
3
Copy n bytes
Transfer completes and dest is returned.
Transfer
=
📋
Destination updated
Memory rearranged safely—even when src and dest share the same array.
Important
📝 Notes
memmove is always safe for overlap; memcpy is not.
Destination must still have room for n bytes—overflow rules apply like memcpy.
For C-strings, copy strlen(s) + 1 unless you intentionally handle termination separately.
When shifting right, ensure the buffer is large enough for the expanded content.
For disjoint copies where performance is critical, memcpy may be preferred after proving no overlap.
Performance
⚡ Optimization
memmove() may add a small overhead compared to memcpy() because it must handle overlap. Use memcpy in hot paths only when you have proven the regions are disjoint. Otherwise, the safety of memmove outweighs the tiny cost difference in most applications.
Wrap Up
Conclusion
memmove() is the overlap-safe memory copy in C. Reach for it when editing strings in place, compacting buffers, or whenever source and destination might alias the same memory. Keep using memcpy() for separate, non-overlapping blocks when you need maximum clarity about intent.
Practice the in-place shift examples, then continue to memset() for filling memory with a byte value.
Use these points whenever you copy or shift memory in C.
5
Core concepts
📋01
Copy n Bytes
Like memcpy.
Basics
🔒02
Overlap Safe
Main advantage.
Safety
🔢03
Shift In Place
Same buffer.
Pattern
📈04
+1 for \0
String copies.
Strings
💬05
vs memcpy()
Disjoint only.
Compare
❓ Frequently Asked Questions
memmove() copies n bytes from src to dest, like memcpy(), but works correctly even when the two regions overlap. It returns dest. Use it when source and destination may share the same buffer or partially overlap.
void* memmove(void* dest, const void* src, size_t n); Include <string.h>. dest is the destination buffer, src is the source, and n is the number of bytes to copy.
memcpy() requires non-overlapping regions—overlap is undefined behavior. memmove() detects overlap and copies in the safe direction (forward or backward) so data is not clobbered before it is read.
Use memmove when copying within the same array, shifting text left or right, or whenever you are not 100% sure the regions are disjoint. Use memcpy only when regions definitely do not overlap—it may be slightly faster.
Copy strlen(src) + 1 bytes to include the null terminator: memmove(dest, src, strlen(src) + 1). Ensure dest has enough space. When src and dest are the same buffer and overlap, memmove is required.
It returns dest—the destination pointer you passed in. The return value matches memcpy's behavior and is mainly useful for chaining copies in advanced code.
Did you know?
On many platforms, memmove checks whether dest falls inside the source range and chooses a forward or backward copy. When there is no overlap, it may delegate to the same fast path as memcpy—so using memmove everywhere is often reasonable for clarity.