Here memcpy and memmove function of the simulation implementation, need to use a null pointer to pass the parameter, after the forced type conversion to char type, with SIZE_T this macro accept offset offset, the simulation is implemented as follows:
memcpy function:
void* my_memcpy (Void*dst,const void*src, size_t count) {assert (DST); assert (SRC); void* ret = Dst;while (count--) {* (char* DST = * (char*) SRC;DST = (char*) DST + 1;src = (char*) src + 1;} return ret;}
Memmove function:
The MEMMVE function is to avoid the memory overlap of the memcpy function (mentioned above), and the development of a new function, the idea is that if there is memory overlap, then we copy from the back, if not, as memcpy, the former back copy, the implementation code is as follows:
void* My_memmove (VOID*DST, const VOID*SRC, size_t count) {assert (DST); assert (src); void*ret = Dst;if ((char*) DST > (( char*) src + count)//determine if memory overlap {while (count--) {* ((char*) DST + count) = * ((char*) src + count);}} Else{while (count--) {* (char*) DST = * (char*) SRC;DST = (char*) DST + 1;src = (char*) src + 1;}} return ret;}
A copy of the memory, more than a copy of the string copy offset parameter, because the string copy is often encountered 0 stop copy, and memory copy does not exist such problems, the use of these two kinds of functions need to think how to effectively let the program run.
If there is any shortage, I hope to criticize.
This article is from the "Pawnsir It Road" blog, so be sure to keep this source http://10743407.blog.51cto.com/10733407/1714495
C language Simulation implements Memcpy,memmove function