Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C library function - memmove()

C library function - memmove()

C standard library - string.h C standard library - string.h


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():


Function declaration

void *memmove(void *str1, const void *str2, size_t n)

Parameter Description

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:

  1. In Scenario 1,dest and src are two independent arrays with no overlap,memmove(dest, src, 9) data to the child component via src the first 9 bytes of the source are copied unchanged to dest.
  2. In Scenario 2,str + 2(destination) and str(source) point to overlapping regions within the same array,memmove saves the data that would be overwritten before completing the copy, so the result is correct.
  3. If Scenario 2 used memcpyinstead, some compilers or platforms may produce incorrect results, because memcpy does 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:

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:


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 C standard library - string.h