These two functions are used to copy a string or a contiguous memory, a function prototype:
void * memcpy (void * destination, const void * source, size_t num);
void * Memmove (void * destination, const void * source, size_t num);
Here's one thing to note: Num refers to the number of bytes that need to be copied, so be sure to consider recalculating the number of copies of the void* when transforming it into a real type.
For example, to convert to Word type, the number of units that actually need to be copied NUM/2
See GLIBC for the implementation of these two functions:
void* memmove (void* dest, const void* SRC, size_t len) {unsigned long int dstp = (long int) dest; unsigned long int SRCP = (long int) src; /* This test makes the forward copying code is used whenever possible. Reduces the working set. */if (DSTP-SRCP >= len)/* *unsigned* compare! */{/* Copy from the beginning to the end. */* If there not too few bytes to copy, use Word copy. */if (len >= op_t_thres) {/* Copy just a few bytes to make DSTP aligned. */Len-= (-DSTP)% Opsiz; BYTE_COPY_FWD (DSTP, SRCP, (-DSTP)% opsiz); /* Copy whole pages from SRCP through DSTP by virtual address manipulation, as much as possible. */Page_copy_fwd_maybe (DSTP, SRCP, Len, Len); /* Copy from SRCP to DSTP taking advantage of the known alignment of DSTP. Number of bytes remaining is put in the third argument, i.e. in LEN. This number is vary from machine to machine. */WORD_COPY_FWD (DSTP, SRCP, Len, Len); /* Fall out and copy the tail. */}/* There is just a few bytes to copy. Use byte memory operations. */BYTE_COPY_FWD (DSTP, SRCP, Len); } else {/* Copy from the end to the beginning. */SRCP + = Len; DSTP + = Len; /* If There not too few bytes to copy, use Word copy. */if (len >= op_t_thres) {/* Copy just a few bytes to make DSTP aligned. */len-= dstp% Opsiz; BYTE_COPY_BWD (DSTP, SRCP, dstp% opsiz); /* Copy from SRCP to DSTP taking advantage of the known alignment of DSTP. Number of bytes remaining is put in the third argument, i.e. in LEN. This number is vary from machine to machine. */WORD_COPY_BWD (DSTP, SRCP, Len, Len); /* Fall out and copy the tail. */}/* There is just a few bytes to copy. Use byte memory operations. */BYTE_COPY_BWD (DSTP, SRCP, Len); } return dest;}
void *memcpy (void* dst, const void* SRC, size_t len) {unsigned long int dstp = (long int) DST; unsigned long int SRCP = (long int) src; /* Copy from the beginning to the end. */* If there not too few bytes to copy, use Word copy. */if (len >= op_t_thres) {/* Copy just a few bytes to make DSTP aligned. */Len-= (-DSTP)% Opsiz; BYTE_COPY_FWD (DSTP, SRCP, (-DSTP)% opsiz); /* Copy whole pages from SRCP through DSTP by virtual address manipulation, as much as possible. */Page_copy_fwd_maybe (DSTP, SRCP, Len, Len); /* Copy from SRCP to DSTP taking advantage of the known alignment of DSTP. Number of bytes remaining is put in the third argument, i.e. in LEN. This number is vary from machine to machine. */WORD_COPY_FWD (DSTP, SRCP, Len, Len); /* Fall out and copy the tail. */}/* There is just a few bytes to copy. Use byte memory operations. */BYTE_COPY_FWD (DSTP, SRCP, Len); return DST;}
It can be found that memcpy is less than memmove to check DESTP-SRCP >= Len, which brings the advantage of Memmove: You can handle the situation where the destination address overlaps the source address!
The system's built-in memmove and memcpy are optimized for use in assemblies, which can be written when they are implemented:
void* n_memmove (void *dst, const void *SRC, size_t len) {char* DSTP = (char*) dst;char* SRCP = (char*) src;if (len = = 0) ret Urn Dst;assert (DST! = NULL && src! = null); if (DSTP-SRCP >= len) {//byte_copy_forward (DSTP, SRCP, Len); for (i NT i = 0; i < Len; i++) Dstp[i] = Srcp[i];} else {//copy from the end to the BEGINNING//BYTE_COPY_BWD (DSTP, SRCP, Len); for (int i = len-1; I >= 0; i--) dstp[i] = Srcp[i];} return DST;} void* n_memcpy (void* DST, const void* SRC, size_t len) {char* DSTP = (char*) dst;char* SRCP = (char*) src;assert (DST! = NULL && src = NULL); if (len = = 0) return Dst;//byte_copy_forwar (DSTP, SRCP, Len); for (int i = 0; i < len; i++) DST P[i] = Srcp[i];return DST;}
C + + memmove and memcpy