Implement a memcpy function.
The memcpy function is used to copy the resource memory (the area of memory pointed to by SRC) to the target memory (the area of memory pointed to by dest);
How many copies? There is a size variable that controls the number of bytes copied.
Function prototypes: void *memcpy (void *dest, void *src, unsigned int count);
Usage: (1) You can copy any type of object, because the parameter type of the function is void* (undefined type pointer), that is, the arguments passed in can be int*,short*,char*, etc.
However, since the process of copying a function is a byte-by-byte copy, the actual operation should be forced to convert void* to char*, so that when the pointer is added, it is guaranteed to add one byte at a time.
At first we would probably write the following error code:
void memcpy (void *dest, void *src, int len) { void *p = dest; void *q = src; if (dest = = NULL | | src = = NULL) { return; } for (int i=0; i<len; i++) { *p++ = *q++; }}
The above error code causes:
- Function prototype should be void*memcpy (void* dest,const void* src,size_t count)
- In accordance with the ANSI (American National standards Institute) standard, you cannot perform algorithmic operations on void pointers, that is, you cannot perform operations such as p++ on void pointers, so you need to convert to specific type pointers, such as char *.
- memcpy is operating on memory and may experience memory overlap, the same problem exists in Memmove, but the two functions in the source code are handled differently: the regions in Dest and source in memcpy cannot overlap, otherwise an unknown result can occur if the area in the dest and source overlap in an unknown result. function does not do any memory processing, memory overlap by the programmer's own control. in memmove, the memory overlap is determined, and when the memory overlaps, it is copied from the back to ensure that replication is handled properly.
consider the memory overlap situation:1. If there is no memory overlap, copy from low address to high address2. If there is memory overlap, copy from high address to low addressthe code is as follows: (this is actually memmove code)
void *memcpy (void *dst, const void *SRC, size_t len) {if (NULL = = DST | | Null = = src) { return null;} void *ret = DST; if (DST <= src | | (char *) DST >= (char *) src + len) { //no memory overlap, start copying from low address while (len--) { * (char *) DST = * (char *) src; DST = (char *) DST + 1; src = (char *) src + 1; } }else{ //memory overlap, starting from high address copy src = (char *) src + len-1; DST = (char *) DST + len-1; while (len--) { * (char *) DST = * (char *) src; DST = (char *) dst-1; src = (char *) src-1; } } return ret;}
The difference between memcpy and memmove:
memcpy and Memmove () are library functions in the C language, in the header file string.h, the role is to copy the contents of a certain length of memory, the prototype is as follows:
void *memcpy (void *dst, const void *SRC, size_t count);
void *memmove (void *dst, const void *SRC, size_t count);
The effect is the same, the only difference is that when memory local overlap, memmove guarantee that the result of the copy is correct, memcpy does not guarantee that the results of the copy is correct.
In case one, the copy overlapping area does not have the problem, the content can be copied correctly.
In case two, the problem appears on the right two bytes, and the original contents of the two bytes are overwritten first and are not saved. So the next copy, the copy is already covered content, obviously this is problematic.
In fact, memcpy is only a subset of the memmove.
memcpy does not consider how memory overlap is implemented:
void *memcpy (void *dest, const void *SRC, unsigned int count) {assert ((dest! = null) && (src! = null)); void *addr ESS = dest; while (count---) {* (char *) dest = * (char *) src; dest = (char *) dest + 1; src = (char *) src + 1;} return address;}
C + + Prime Learning Essentials implementation of memcpy library functions