memcpy:void *memcpy (void *dest, const void *SRC, size_t n);
Memmove:void *memmove (void *dest, const void *SRC, size_t n);
Two functions are defined in string.h
memcpy: Copies n bytes from the starting position of the memory address referred to by the source SRC to Target Dest in the starting position of the memory address referred to
The memory areas referred to by 1.source and dest can overlap, but if the memory areas referred to by source and dest overlap, this function does not ensure that the overlapping area of the source is overwritten before the copy. The use of memmove can be used to process overlapping areas. The function returns a pointer to Destin.
2.strcpy and memcpy mainly have the following 3 aspects of the odd.
2.1, the content of the copy is different. strcpy can only copy strings, while memcpy copies arbitrary content, such as character arrays, integers, structs, classes, and so on.
2.2, the method of replication is different. strcpy does not need to specify a length, it encounters the string terminator of the copied character "\" before it ends, so it is prone to overflow. memcpy determines the length of the copy according to its 3rd parameter.
2.3, the use of different. It is common to use strcpy when copying strings and to replicate other types of data memcpy
3. If the target array Destin itself already has data, after executing memcpy (), the original data will be overwritten (up to N). If you want to append data, add the destination array address to the address you want to append the data to after each execution of memcpy.
Note: Both source and Destin are not necessarily arrays, and any readable and writable space is available.
void * __cdecl memcpy ( void * dst,const void * src,size_t count) { void * ret = dst; while (count--) { // Note that the memcpy function does not address the issue of whether the DST and src areas overlap * (char *) dst = * (char *) src; dst = (char *) dst + 1; src = (char *) src + 1; } return (ret);} void * __cdecl memmove (&NBSP;VOID&NBSP;*&NBSP;DST,CONST&NBSP;VOID&NBSP;*&NBSP;SRC, Size_t count) { void * ret = dst; if (dst <= src | | (char *) dst >= ((char *) src + count)) { // if the DST and src areas do not overlap, copy them from the beginning while (count--) { * (char *) dst = * (char *) src; dst = (char *) dst + 1; src = (char *) src + 1; } } else { // if the DST and src areas intersect, copy from the tail to the start position, which avoids data conflicts dst = (char *) DST + count - 1; src = (char *) src + count - 1; while (count--) { * (char *) dst = * (char *) src; dst = (char *) dst - 1; src = (char *) src - 1; } } return (ret);}
This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1755346
memcpy and Memmove