C language-memory copy handler for strings
For strings, we use the str class function in the string <string. h>, but we also have some copy functions about memory. They operate on the memory and can then copy any type of data. This is <memory. h> In memcpy, let's take a look at MSDN and see its prototype: void * memcpy (void * dest, const void * src, size_t count ); different from strcpy, a third parameter is added to determine the number of bytes for the operation, and the parameter type and return type are all void *, which means that it can copy any type of data. Then let's take a look at the implementation: memcpy:
Void * my_memcpy (void * str, const void * Dstr, int count) // change the memory address and determine the length, therefore, the {char * pstr = (char *) str; char * pDstr = (char *) Dstr; assert (str! = NULL) & (Dstr! = NULL); if (str = Dstr) // return the return (char *) Dstr to be changed if the location is the same; while (count --> 0) {* pstr ++ = * pDstr ++;} return str ;}
Then there will be a problem. If the starting position of the Dstr In the copied data is between the STR operations, there will be side effects when str is changed, which will lead to incorrect copying results, therefore, we should consider the coverage. There is a memmove function in the function library. Memmove:
void *my_memmove(void *pst,const void *Dpst,int size){ void *p = pst; char *pstA = (char *)pst; char *pstB = (char *)Dpst; assert((pst != NULL) &&(Dpst != NULL)); if(pstB<pstA< pstB+size) { while(size--) { *(pstA+size) = *(pstB+size); } } else { while(size--) { *pstA++ = *pstB++; } } return p;}
It means that the copied space starts in the copy space and overwrites the copy memory. In this case, we will consider copying from the end. So I made a judgment.