The memcopy and Memmove functions look at the source code of two functions under Linux.
Two functions are defined in the header file string.h, and the function prototype is:
void * __cdecl memcpy (void * dst,const void * src,size_t count);
void * __cdecl memmove (void * dst,const void * SR c,size_t count);
The implementation code is as follows:
void * __cdecl memcpy (void * dst,const void * src,size_t count) {void * ret = DST;
while (count--) {//Note that the memcpy function does not handle the problem of overlapping DST and SRC regions * (char *) DST = * (char *) src;
DST = (char *) DST + 1;
src = (char *) src + 1;
return (ret);
} void * __cdecl memmove (void * dst,const void * src,size_t count) {void * ret = DST; if (DST <= src | | (char *) DST >= ((char *) SRC + count) {//If the DST and src regions do not overlap, copy them at the beginning WH
Ile (count--) {* (char *) DST = * (char *) src;
DST = (char *) DST + 1;
src = (char *) src + 1; } else {//if the DST and SRC regions Cross, copy from the tail to the starting position to avoid data collisions DST = (char *) DST + cou
nt-1;
src = (char *) src + count-1; While (count--)
{* (char *) DST = * (char *) src;
DST = (char *) dst-1;
src = (char *) src-1;
} return (ret); }
To sum up:
When the SRC and DST regions do not overlap, two functions are exactly the same. The wood has overlapping conditions: DST <= src | | (char *) DST >= ((char *) SRC + count. Otherwise, memcpy is not working properly, memmove can work normally.