memcpy VS memmove
2009-10-04 22:27
246 查看
memcpy和memmove函数的实现
区别: 从DESCRIPTION看来,两者的功能基本相同,唯一不同的是,当 dest 和 src 有重叠的时候选用不同的函数可能会造成不同的结果。不妨写个小程序来测一下: #include <string.h> #include <stdio.h> int main() { int i = 0; int a[10]; for(i; i < 10; i++) { a[i] = i; } memcpy(&a[4], a, sizeof(int)*6); for(i = 0; i < 10; i++) { printf("%d ",a[i]); } printf("/n"); return 0; } 很简单的小程序!不过已经足以达到我的目的了:)将上面代码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; } |
相关文章推荐
- memmove VS memcpy
- strcpy vs memcpy vs memmove
- memcpy vs memmove
- strcpy/memcpy/memmove三者的区别
- memcpy memmove区别和实现
- memcpy memmove区别和实现
- strcpy/memcpy/memmove的实现
- mem系列函数(memset memcpy memmove) 和str系列函数(strlen strcpy strcmp strcat strstr strtok)
- memcpy memmove区别和实现
- 常用库函数编程实现用法总结(一)memmove memcpy strcpy strncpy
- memcpy memmove 区别
- memmove .memset .memcpy
- memcpy/memmove实现
- memcpy memmove区别和实现
- 字符串函数(strcpy strncpy strcat strncat strcmp strncmp memcpy memmove)
- memmove memcpy
- memmove and memcpy
- C/C++之Memcpy and memmove
- c memmove和memcpy的实现和区别
- memcpy memmove区别和实现