Differences:
From the description perspective, the two functions are basically the same. The only difference is that when DEST and SRC overlap, selecting different functions may lead to different results. Write a small program to test it:
0 # I nclude <string. h>
1 # I nclude <stdio. h>
2
3 int main ()
4 {
5 Int I = 0;
6 int A [10];
7
8 For (I; I <10; I ++)
9 {
10 A [I] = I;
11}
12
13 memcpy (& A [4], A, sizeof (INT) * 6 );
14
15 For (I = 0; I <10; I ++)
16 {
17 printf ("% d", a [I]);
18}
20
21 printf ("/N ");
22 return 0;
23}
Very simple mini-program! But it is enough to achieve my goal: run the above Code GCC and then the result is: 0 1 2 3 0 1 2 3 0 1.
Replace row 13th with: memmove (& A [4], A, sizeof (INT) * 6), run the GCC command again, and the result is: 0 1 2 3 0 1 2 3 4 5!
Haha, there is a difference between the two. But this is not enough. Continue to modify 13 rows: memmove (A, & A [4], sizeof (INT) * 6) // Replace the source and target.
Re-compile and run GCC. The result is 4 5 6 7 8 9 6 8 9.
Not enough. Continue to modify 13 behavior: memcpy (A, & A [4], sizeof (INT) * 6); run GCC and the result is still: 4 5 6 7 8 9 6 7 8 9!
Now the truth is clear. Comparing the above four results, it is difficult to draw the following conclusions:
1. when the SRC and DEST memory areas overlap, memmove can ensure that the first n Bytes of the SRC memory area can be correctly copied to the memory in the Dest;
2. When the SRC address is lower than the Dest address, the results are the same. In other words, the difference between memmove and memcpy is only reflected in the case that the head of DeST overlaps with the tail of SRC;
Memcpy
Code:
;***
; Memcpy. ASM-contains memcpy and memmove routines
;
; Copyright (c) 1986-1997, Microsoft Corporation. All right reserved.
;
; Purpose:
; Memcpy () copies a source memory buffer to a destination buffer.
; Overlapping buffers are not treated specially, so propogation may occur.
; Memmove () copies a source memory buffer to a destination buffer.
; Overlapping buffers are treated specially, to avoid propogation.
;
; **************************************** ***************************************
;***
; Memcpy-copy source buffer to destination Buffer
;
; Purpose:
; Memcpy () copies a source memory buffer to a destination memory buffer.
; This routine does not recognize overlapping buffers, and thus can lead
; To propogation.
; For cases where propogation must be avoided, memmove () must be used.
;
; Algorithm:
Void * memcpy (void * DEST, void * Source, size_t count)
{
Void * ret = DEST;
// Copy from lower address to higher address
While (count --)
* DEST ++ = * source;
Return ret;
}
Memmove
Memmove-copy source buffer to destination Buffer
;
; Purpose:
; Memmove () copies a source memory buffer to a destination memory buffer.
; This routine recognize overlapping buffers to avoid propogation.
; For cases where propogation is not a problem, memcpy () can be used.
;
; Algorithm:
Void * memmove (void * DEST, void * Source, size_t count)
{
Void * ret = DEST;
If (DEST <= source | DEST> = (source + count ))
{
// Non-overlapping Buffers
// Copy from lower addresses to higher addresses
While (count --)
* DEST ++ = * Source ++;
}
Else
{
// Overlapping Buffers
// Copy from higher addresses to lower addresses
DeST + = count-1;
Source + = count-1;
While (count --)
* Dest -- = * source --;
}
Return ret;
}