Memmove and memcpy
1.memmove
Function prototypes: void *memmove (void *dest, const void *source, size_t count)
Return value Description: Returns the void * pointer to dest
Parameter description: Dest,source is the destination string and the first address of the source string, respectively. Count is the number of characters to move
Function Description: Memmove is used to copy count characters from source to dest, and if the target region and source area overlap, memmove can ensure that the source string copies the bytes of the overlapping region to the target area before being overwritten.
2.memcpy
Function prototypes: void *memcpy (void *dest, const void *source, size_t count);
Return value Description: Returns the void * pointer to dest
Function Description: The memcpy function and Memmove are the same, but the regions in dest and source in memcpy cannot overlap, otherwise unknown results occur.
3. Difference between the two
The function memcpy () copies count characters from the area to which the source points to dest, and does not define the behavior of the function if the two arrays overlap.
and Memmove (), if the two functions overlap, the assignment is still correct.
The memcpy function assumes that there is no overlap in the area of memory to be copied, and if you can make sure that the memory area of your copy operation does not overlap, you can use memcpy directly;
If you cannot guarantee that there is overlap, in order to ensure the correctness of the copy, you must use Memmove.
memcpy will be more efficient than memmove, and if you don't understand it, you can see some of the two implementations:
Memmove
void*memmove (void*dest,void Const*src, size_t N) {RegisterChar*DP =dest; RegisterChar*SP =dest; if(DP <sp) { while(n-->0) *dp++ = *sp++; } Else { //we should do the copy reverselyDP + =N; SP+=N; while(n-->0) *--DP = *--sp; } }
memcpy
void *memcpy (voidconstvoid *src, size_t count) { char *tmp = dest; Const char *s = src; while (count--) *tmp++ = *s++; return dest;}
STRCMP implementation
intstrcmp (Const Char* SRC,Const Char*DST) { intRET =0 ; while( ! (ret = * (unsignedChar*) src-* (unsignedChar*) DST) && *DST)++SRC, + +DST; if(Ret <0) ret= -1 ; Else if(Ret >0) ret=1 ; return(ret);}
strcpy implementation
Char* strcpy (charconstChar*char* tmp =while '+') return dest;}
Implementation of several library functions for Memmove and memcpy and strcmp strcpy