Int arr [4]={
9, 5, 2, 7
};
Memcpy (arr + 1, arr, sizeof (int) * 2);
For (int I=0; I & lt; Sizeof (arr)/sizeof (arr [0]). + + I) {
Printf (" % d ", arr [I]);
}
printf("\n");
Let's take a look at the memcpy source look at is how to copy data:
/* * *
* memcpy. C - the contains memcpy routine
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* the Purpose:
* memcpy () copies a source memory buffer to a destination buffer.
* Overlapping buffers are not treated specially, so propogation may occur.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include
#include
# pragma function (memcpy)
/* * *
* memcpy - Copy the source buffer to destination buffer
*
* the Purpose:
* memcpy () copies a source memory buffer to a destination buffer memory.
* This routine does NOT recognize overlapping buffers, and thus can lead
* to propogation.
*
* For cases where propogation must be avoided, memmove () must be 2.
*
* Entry:
* void * DST=pointer to destination buffer
* const void * SRC=https://bbs.csdn.net/topics/pointer to source buffer
* size_t count=number of bytes to copy
*
* the Exit:
* Returns a pointer to the destination buffer
*
* Exceptions:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Void * __cdecl memcpy (
Void * DST,
Const void * SRC,
Size_t count
)
{
Void * ret=DST;
/*
* copy from the lower addresses to who addresses
*/
While (count) {
* (char *) DST=* SRC (char *);
DST=DST + 1 (char *);
SRC=https://bbs.csdn.net/topics/(char *) SRC + 1;
}
Return (ret);
}
As you can see, memcpy is a byte of data from the source address space of a byte copy to the destination address space , so, the for example, in front of us run results should be :
9 September 9 7
However, the actual operation result is as follows:
Why is this? Is the compiler has been optimized, I tried the vc + +, and MinGW GCC front said there was no memory overlap will appear problem ,
Then, I went to checked the official documentation, official document of the following :
Official documentation, memory overlap, use memcpy belongs to undefined behavior,
Official document said belong to undefined behavior, but multiple compiler to complete copy right, this is why ?
If can complete copy right memcpy, memcpy and memmove and what's the difference between ?
CodePudding user response:
You put the memcpy code is only one instance
Actual memcpy implementation is much more complex is used a lot of optimization
CodePudding user response:
Library function to realize the two address size may pass judgment, do positive sequence or reverse copying, but before implementation is directly USES the assembly instructions to do, is also the highest efficiency, address overlap problems inevitablyCodePudding user response:
Are not properly completed copy, you can try more data, will go wrong, just because you now the test data of too little, just rightCodePudding user response: