1. void *mymemcpy(void *dest, const void* src, size_t n);
内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中
1 void* memcpy(void* dest,void* src,size_t n){ 2 assert(dest != NULL && src != NULL); 3 4 if(src < dest && (char*)src + n > dest){ 5 char* pdst = (char*)dest+n-1; 6 char* psrc = (char*)src+n-1; 7 while(n--){ 8 *pdst-- = *psrc--; 9 } 10 }else{ 11 char* pdest = (char*)dest; 12 char* psrc = (char*)src; 13 while(n--){ 14 *pdest++ = *psrc++; 15 } 16 } 17 return dest; 18 }
2.void *memset(void *s, int c, size_t n);
1 void* memset(void* src,int c,size_t n){ 2 assert(src != NULL); 3 char* psrc = (char*)src; 4 while(n--){ 5 *psrc++ = (char)c; 6 } 7 return src; 8 }
3. char* strcpy(char* dest,const char* src);
1 char* strcpy(char* dest,const char* src){ 2 assert(dest != NULL && src != NULL); 3 4 size_t n = strlen(src); 5 if(src < dest && src + n > dest){ 6 char* pdst = dest + n; 7 *pdst-- = '