memcpy和memmove的区别

来源:互联网 发布:动态导入java 编辑:程序博客网 时间:2024/05/22 14:15

memcpy和memmove的区别就是:

memcpy不支持目标和源内存区域重叠

memmove则支持

 

#ifndef __HAVE_ARCH_MEMCPY/** * memcpy - Copy one area of memory to another * @dest: Where to copy to * @src: Where to copy from * @count: The size of the area. * * You should not use this function to access IO space, use memcpy_toio() * or memcpy_fromio() instead. */void *memcpy(void *dest, const void *src, size_t count){char *tmp = dest;const char *s = src;while (count--)*tmp++ = *s++;return dest;}EXPORT_SYMBOL(memcpy);#endif#ifndef __HAVE_ARCH_MEMMOVE/** * memmove - Copy one area of memory to another * @dest: Where to copy to * @src: Where to copy from * @count: The size of the area. * * Unlike memcpy(), memmove() copes with overlapping areas. */void *memmove(void *dest, const void *src, size_t count){char *tmp;const char *s;if (dest <= src) {tmp = dest;s = src;while (count--)*tmp++ = *s++;} else {tmp = dest;tmp += count;s = src;s += count;while (count--)*--tmp = *--s;}return dest;}EXPORT_SYMBOL(memmove);#endif


因此,在不确定拷贝内存区域是否重叠时,最好用memmove。

原创粉丝点击