C library function - memmove()
memmove() is a memory-operation function in the C standard library used to copy a specified number of bytes from a source address to a destination address, and able to safely handle overlapping memory regions.
Word meaning:memmove Is memory moveshort for (memory move), meaning to move data in memory.
C Library Function void *memmove(void *str1, const void *str2, size_t n) From the str2 Copy n bytes to str1.
Differences from memcpy():
- When the destination region overlaps the source region,
memmove()guarantees that the overlapping bytes are safely transferred to the destination before the source data is overwritten, so it is more reliable thanmemcpy()more reliable. - If the destination and source regions do not overlap at all,
memmove()behavior is the same asmemcpy(). - When it is uncertain whether two memory regions overlap, prefer to use
memmove().
Function declaration
void *memmove(void *str1, const void *str2, size_t n)
Parameter Description
- str1: destination address, pointing to the memory area where the content is to be stored (type-cast to
void*). - str2: source address, pointing to the start of the data to be copied (type-cast to
const void*). - n: the number of bytes to copy (
size_ttype).
Return Value
Returns a pointer to the destination memory area str1 (the same as the pointer passed in). str1 ).
Header File
#include
Example
Example 1: basic usage
The following example demonstrates memmove() Usage in both non-overlapping and overlapping scenarios:
Example
#include
#include
int
main
(
)
{
/* Scenario 1: non-overlapping memory copy */
char
dest
[
]
=
"oldstring"
;
char
src
[
]
=
"newstring"
;
printf
(
"Before non-overlapping copy: dest = %s, src = %s\n"
, dest, src
)
;
memmove
(
dest, src,
9
)
;
printf
(
"After non-overlapping copy: dest = %s, src = %s\n"
, dest, src
)
;
/* Scenario 2: overlapping memory copy (where memmove excels) */
char
str
[
]
=
"123456789"
;
printf
(
"\nBefore overlapping copy: %s\n"
, str
)
;
memmove
(
str
2
, str,
6
)
;
printf
(
"After overlapping copy: %s\n"
, str
)
;
return
0
;
}
Running result:
非重叠拷贝前:dest = oldstring, src = newstring
非重叠拷贝后:dest = newstring, src = newstring
重叠拷贝前:123456789
重叠拷贝后:121234569
Code analysis:
- In Scenario 1,
destandsrcare two independent arrays with no overlap,memmove(dest, src, 9)data to the child component viasrcthe first 9 bytes of the source are copied unchanged todest. - In Scenario 2,
str + 2(destination) andstr(source) point to overlapping regions within the same array,memmovesaves the data that would be overwritten before completing the copy, so the result is correct. - If Scenario 2 used
memcpyinstead, some compilers or platforms may produce incorrect results, becausememcpydoes not guarantee handling of overlapping cases.
Example 2: Handling overlapping memory
When two memory regions overlap,memmove() still gives the correct result:
Example
#include
#include
int
main
(
)
{
char
str
[
]
=
"memmove can be very useful"
;
printf
(
"Before: %s\n"
, str
)
;
/* Copy 7 bytes starting at position 11 to position 5; the regions overlap */
memmove
(
str
5
, str
11
,
7
)
;
printf
(
"After: %s\n"
, str
)
;
return
0
;
}
Running result:
Before: memmove can be very useful
After: memmove can be very useful
Code analysis:
- In the original string, starting at position 5 is
"ve can be very useful"and the 7 bytes starting at position 11 are"can be"(including the trailing space). memmove(str + 5, str + 11, 7)data to the child component via"can be"Moving to position 5, the two regions overlap, butmemmovechooses a forward or backward copy strategy based on the addresses, ensuring data integrity.- After the copy, the content starting at position 5 becomes
"can be"and the remaining characters are unchanged.
Example 3: Comparing memcpy() and memmove()
The following example shows the different results the two functions may produce in the same overlapping scenario:
Example
#include
#include
int
main
(
)
{
char
str1
[
]
=
"abcdefghij"
;
char
str2
[
]
=
"abcdefghij"
;
printf
(
"Original: %s\n"
, str1
)
;
/* Using memcpy: behavior is undefined when regions overlap */
memcpy
(
str1
2
, str1,
5
)
;
printf
(
"memcpy: %s\n"
, str1
)
;
/* Using memmove: behavior is guaranteed when regions overlap */
memmove
(
str2
2
, str2,
5
)
;
printf
(
"memmove: %s\n"
, str2
)
;
return
0
;
}
Running result:
Original: abcdefghij
memcpy: ababababij
memmove: ababcdfghij
Description: In this example,memcpy copies byte by byte in the forward direction, causing the source data to be overwritten early and producing unexpected output; whereas memmove detects the overlap and copies backward, producing the correct result. Different platforms or compilers may implement memcpy it differently, so always prefer this function in overlapping scenarios. memmove.
Example 4: Copying a specified number of bytes
memmove() is a byte-level operation and does not automatically write a string terminator; you must add it manually. '\0':
Example
#include
#include
int
main
(
)
{
char
src
[
]
=
"Hello, World!"
;
char
dest
[
20
]
;
/* Copy only the first 5 bytes */
memmove
(
dest, src,
5
)
;
dest
[
5
]
=
'\0'
;
/* Manually add the string terminator */
printf
(
"Source: %s\n"
, src
)
;
printf
(
"Dest: %s\n"
, dest
)
;
printf
(
"Length: %zu\n"
,
strlen
(
dest
)
)
;
return
0
;
}
Running result:
Source: Hello, World!
Dest: Hello
Length: 5
Code analysis:
memmove(dest, src, 5)Copy only the first 5 bytes ('H' 'e' 'l' 'l' 'o').memmove()does not automatically write'\0'at the destination; if you omitdest[5] = '\0'subsequent string operations (such asprintf,strlenwill read indeterminate memory, causing undefined behavior.
Differences from memcpy()
| Feature | memcpy() | memmove() |
|---|---|---|
| Overlapping memory handling | Undefined behavior, may produce incorrect results | Handled safely, guarantees correct copying |
| Implementation | Copies byte by byte directly in order | Detects overlap direction and chooses forward or backward copying |
| Performance | Usually slightly faster (no overlap-detection overhead) | Slightly slower (includes overlap-detection logic) |
| Usage recommendation | Use when source and destination are known not to overlap | Use when regions overlap or overlap is uncertain |
C standard library - string.h