This blog post is mostly about writing memcpy functions in the library,
For strings, we use the function of the str category in string <string.h>, but we also have some copy functions about memory. The object they manipulate is memory and can then accept any type of data for copying.
This is the memcpy in <memory.h>, and then we'll take a look at MSDN and take a look at his prototype:
void *memcpy (void *dest,const void *src,size_t count);
Unlike strcpy, the third parameter is added, the number of bytes of the operation is determined, and then the parameter type and return type are void*
, which means that he can copy any type of data.
Then we look at the implementation:
memcpy
void *my_memcpy (Void *str,const void *dstr,int count) //from the memory address to change, and determine the length of change, so with a universal type to accept { char *pstr = (char *) str; char *pDstr = (char *) dstr; assert ((str!=null) && (dstr != null)); if (STR&NBSP;==&NBSP;DSTR) //in the same positionIn case of a direct return to the change return (char *) dstr; while (count-- > 0) { *pstr++ = *pdstr ++; } return str;}
Then there is a problem, if we copy the data in the DSTR start position between the Str operation, then the change Str will have side effects, will cause our copy result is not correct, so we should consider the situation of 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) { &nbSp; while (size--) { * (PstA+size) = * (pstb+size); } } else { while (size--) { *pstA++ = *pstB++; } } return p;}
The copy memory overlay is encountered when the copied space starts in the copy space. In this case we will consider copying from the tail. So the judgment was made.
This article is from the "egg-left" blog, please be sure to keep this source http://memory73.blog.51cto.com/10530560/1697227
Memory copy processing function for the "C language" string