内存拷贝函数模拟实现

it2025-05-06  23

#include <stdio.h> ●与字符串型的拷贝函数类似的内存拷贝函数 memmove的模拟函数 #include <stdlib.h> #include <string.h> #include <assert.h> #include <iostream> void *my_memmove(void *dst,const void *src,size_t num)//内存拷贝函数模拟实现 { assert(dst!=NULL); assert(src!=NULL); if( dst>src&&dst<src+num) { const char*src_=(char*)src+num-1; char*dst_=(char*)dst+num-1; while(num>0) { *dst_=*src_; dst_--; src_--; num--; } } else { const char*src_=(char*)src; char*dst_=(char*)dst; while(num>0) { *dst_=*src_; dst_++; src_++; num--; } } } int main() { char dst[32]="asdfghjkl"; int len=strlen(dst); my_memmove(dst,dst+3,len); printf("%s",dst); return 0; } 该函数实现了库函数中的memmove函数,总之还是有一些难的,对指针的操作有一定的要求,若对指针不是很熟,就很难搞了。
最新回复(0)