What is the difference between memcpy and memmove ??

Source: Internet
Author: User

# Include <iostream>
Using namespace STD;
Int main ()
{
Int A [10];
For (INT I = 0; I <10; I ++)
A [I] = I;
Memcpy (& A [4], A, sizeof (INT) * 6 );
For (I = 0; I <10; I ++)
Cout <A [I] <Endl;
}

Very simple mini-program! But it is enough to achieve my goal: run the above Code GCC and then the result is: 0 1 2 3 0 1 2 3 0 1.
Replace row 13th with: memmove (& A [4], A, sizeof (INT) * 6), run the GCC command again, and the result is: 0 1 2 3 0 1 2 3 4 5!
Haha, there is a difference between the two. But this is not enough. Continue to modify 13 rows: memmove (A, & A [4], sizeof (INT) * 6) // Replace the source and target.
Re-compile and run GCC. The result is 4 5 6 7 8 9 6 8 9.
Not enough. Continue to modify 13 behavior: memcpy (A, & A [4], sizeof (INT) * 6); run GCC and the result is still: 4 5 6 7 8 9 6 7 8 9!
Now the truth is clear. Comparing the above four results, it is difficult to draw the following conclusions:
1. when the SRC and DEST memory areas overlap, memmove can ensure that the first n Bytes of the SRC memory area can be correctly copied to the memory in the Dest;
2. When the SRC address is lower than the Dest address, the results are the same. In other words, the difference between memmove and memcpy is only reflected in the case that the head of DeST overlaps with the tail of SRC;
Memcpy

Memcpy copies n characters from the source object to the object to which DESTIN points, and returns a pointer to the result object.
Memmove also copies n characters in the object pointed to by source to the object pointed to by Destin, but the process is like copying the object pointed to by source to a temporary array first, then, the pointer to the result object is returned when the temporary array is copied to the object indicated by Destin.
Note that the results of string operation functions except memmove are uncertain when copying characters in the same string. In other words, memmove can copy a part of itself to another part of itself.

The results are the same in VC!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.