memcpy與memmove解析
http://blog.csdn.net/hfw_1987/article/details/4193872
從DESCRIPTION看來,兩者的功能基本相同,唯一不同的是,當 dest 和 src 有重疊的時候選用不同的函數可能會造成不同的結果。不妨寫個小程序來測一下:
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 }
很簡單的小程序!不過已經足以達到我的目的了:)將上面代碼gcc之後再運行,結果爲:0 1 2 3 0 1 2 3 0 1 。
再把第13行改成:memmove(&a[4], a, sizeof(int)*6),重新gcc再運行,結果爲:0 1 2 3 0 1 2 3 4 5 !
呵呵,兩者的區別出現了。不過其實這樣還不夠,繼續修改13行: memmove(a, &a[4], sizeof(int)*6) //也就是將源、目的置換一下而已
重新gcc編譯再運行,結果爲:4 5 6 7 8 9 6 7 8 9 。
還不夠,繼續修改13行爲: memcpy(a, &a[4], sizeof(int)*6); gcc並運行,結果仍爲: 4 5 6 7 8 9 6 7 8 9 !
至此真相已經大白了。對比上面四個結果,不難得出以下結論:
1. 當 src 和 dest 所指內存區有重疊時,memmove 相對 memcpy 能提供保證:保證能將 src 所指內存區的前 n 個字節正確的拷貝到 dest 所指內存中;
2. 當 src 地址比 dest 地址低時,兩者結果一樣。換句話說,memmove 與 memcpy 的區別僅僅體現在 dest 的頭部和 src 的尾部有重疊的情況下;
memcpy
代碼:
;***
;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--;l
}
return ret;
}
另一種實現:
void* mymemcpy( void* dest, const void* src, size_t count )
{
char* d = (char*)dest;
const char* s = (const char*)src;
// int n = (count + 7) / 8; // count > 0 assumed
int n = count >> 3;
switch( count & 7 )
{
do { *d++ = *s++;
case 7: *d++ = *s++;
case 6: *d++ = *s++;
case 5: *d++ = *s++;
case 4: *d++ = *s++;
case 3: *d++ = *s++;
case 2: *d++ = *s++;
case 1: *d++ = *s++;
case 0 } //while (--n > 0);
while (n-- > 0)
}
return dest;
}