数据拷贝的方法解析
memcpy 与 memmove
区别:两个函数都是进行n字节内存内容的拷贝,入口参数和返回参数也都一样,可是这两个函数在内部实现上是有一定区别的,这主要是因为dest内存区域和src内存区域可能有一下四种不同的情况,
注意count的影响:
从图中可以看出,src的内存区域和dest的内存区域相对位置和重叠关系有四种情况,memcpy没有考虑重叠的情况,而memmove考虑到了全部情况,因此memcpy函数的时候可能出现意向不到的结果。
这两个函数的实现:
***********下面两个是错误的实现**************
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;
}
=======================================================================
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;
}
***********************正确的如下**************************
void* mymemcpy(void* dest, void* source, size_t count) {
char *ret = (char *)dest; char *dest_t = ret;
char *source_t = (char *)source;
while (count--){
*dest_t++ = *source_t++; }
return ret;
}
void *my_memmove(void *dst,const void *src,int count) {
char *ret;
char *dst_t;
char *src_t;
ret = (char *)dst;
if ((unsigned char*)
dst <= (unsigned char*)src || (unsigned char *)dst >= ((unsigned char *)src + count))
{
dst_t = (char *)dst;
src_t = (char *)src;
while (count--)
{
*dst_t++ = *src_t++;
}
}else{
dst_t = (char *)dst + count - 1;
src_t = (char *)src + count - 1;
while (count--)
{
*dst_t-- = *src_t--;
}
}
return(ret); }