//7.实现memmove(内存拷贝函数) void* memmove(void* dest,const void* src,size_t num)
//memmove 和memcpy 的差别为:memmove 可以处理源内存块和目标内存块重叠的情况,重叠的话倒着拷贝
void* memmove(void* dest, const void* src, size_t count){
void* ret = dest;
if (dest <= src || (char*)dest >= ((char*)src + count)){
while (count--){
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else{
dest = (char*)dest + count - 1;
src = (char*)src + count - 1;
while (count--){
*(char*)dest = *(char*)src;
dest = (char*)dest - 1;
src = (char*)src - 1;
}
}
}
C 基础 16 模拟实现 memmove
最新推荐文章于 2019-05-29 15:30:21 发布