Void memcpy(void DST, const void * SRC, size_t count) void memcpy(void DST, const void * SRC, size_t count) void memcpy(void DST, const void * SRC, size_t count);

void memmove(void dst, const void *src, size_t count);

They have the same function, the only difference is that memmove guarantees the correct copy of the memory when local overlap occurs, memcpy does not guarantee the correct copy of the memory.

In the first case, there is no problem with copying overlapping areas and the content can be copied correctly. In the second case, the problem arises in the two bytes on the right, where the original contents are overwritten first and are not saved. So the next time you copy it, you’re copying something that’s already been overwritten, so obviously there’s something wrong with that. In fact, Memcpy is just a subset of Memmove.

The C language implementation of the two is very simple, interested friends can go to see. In reality, both of these functions are implemented using assembly.

Char s[] = “1234567890”; char s[] = “1234567890”; char s[] = “1234567890”; char s[] = “1234567890”; char s[] = “1234567890”; char s[] = “1234567890”; char* p1 = s; char* p2 = s+2; The results of memcpy(p2, p1, 5) and memmove(p2, p1, 5) may be different. Memmove () can copy the first five characters of p1 into p2, while the results of memcpy() may not be correct

The Naming of a Metamorphosis

When we write a program, generally pay attention to see the name of the variable, can let others know the basic meaning of the variable. Memcpy memory copy, no problem; Memmove, memory move? No, if you understand this, then you must have a good look at this article, memmove or memory copy. So since memcpy and memmove are both memory copies, what is the difference between them?

Say first memcpy

Have you ever taken a C++ written test? What a common pen test that asks you to write an implementation of Memcpy. Now, grab your calculator paper and pen; Yes, it’s pen and paper, not for you to write on your IDE. Can’t write it? Check it out below:

void *memcpy(void *dest, const void *src, size_t count) { assert(dest ! = NULL || src ! = NULL); char *tmp = (char *)dest; char *p = (char *)src; while (count--) { *tmp++ = *p++; } return dest; }

The implementation of Memcpy is very simple, generally in the written test, the topic of writing source code, nothing more than the need to pay attention to the following points:

1. Determine the function prototype; 2. Judge the legitimacy of parameters; 3. Logical implementation (considering various situations, collectively referred to as logical implementation); Error handling.

Of course, mine has no error handling, and it doesn’t need error handling. Above, I wrote the implementation of the source code of Memcpy, the implementation principle is as follows:

If this goes on, the above code will work fine, but what if the following happens?

I, n, k and J have the same memory address as J, e, l. Now use the above code to copy the memory. Have you ever thought about it? If not, think about it now and don’t rush to read the rest.

Then, I’ll leave you with one more question, why is it necessary to convert void to char in all of the above code? Char * TMP = (char *)dest;

Besides, memmove

MemMove is also used to implement a direct copy of memory. Speaking of this name, I feel how much or a little pit. Since Memmove is also used for memory data movement, then first look at the implementation of the source Memmove.

void *memmove(void *dest, const void *src, size_t count) { assert(dest ! = NULL || src ! = NULL) if (dst < src) { char *p = (char *)dest; char *q = (char *)src; while (count--) { *p++ = *q++; } } else { char *p = (char *)dest + count; char *q = (char *)src + count; while (count--) { *--p = *--q; } } return dest; }

From the source code, Memmove is indeed more complex than Memcpy; A closer look, what more? Oh, there’s an else branch, and it’s this else branch that handles the problem when SRC and dest overlap.

Memcpy vs. Memmove

When the SRC and dest memory overlap, memmove handles the copy from the back to the front. Of course, the problem of overlap, need to consider the following two situations.

As shown in the figure, when the corresponding situation (1) occurs, it is necessary to copy from the head of SRC first; The if branch of the memmove source code is consistent with the implementation of memcpy. When the corresponding situation of (2) occurs, it is necessary to copy from the end of SRC first to prevent overwriting phenomenon. This is one of the more important considerations of Memmove than Memcpy, so it is safer to use Memmove than Memcpy in real life.