Home > Back-end >  Why, when memory overlap memcpy copy will not be a problem, where is the difference between it and m
Why, when memory overlap memcpy copy will not be a problem, where is the difference between it and m

Time:06-05

Correlation function in learning C language of memory, I remember the teacher said, memcpy and memmove difference is when memory overlap occurs, memcpy cause problems , specific code is as follows:
 
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 inevitably

CodePudding 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 right

CodePudding user response:

reference 1st floor lin5161678 response:

You put the memcpy code is only one instance
Actual memcpy implementation is much more complex is used a lot of optimization

You this is what the compiler?

CodePudding user response:

Undefined behavior is not necessarily wrong, but does not guarantee that he is right, the program can be deadly

CodePudding user response:

Give you a hint:
You define data is: int (signed, 4 bytes)
Memcpy operation is: char (1 byte, not necessarily a symbol)

CodePudding user response:




The
refer to 6th floor to the scarecrow to reply:
give you a hint:
You define data is: int (signed, 4 bytes)
Memcpy operation is: char (1 byte, not necessarily a symbol)

Bosses, give hints about bai, don't know much about

CodePudding user response:

Let me put it this way, you are a bit to drill the wrong direction, memcpy is not suitable for integer operation,
Int a [0]=9;
Stored in the memory is 32 bits: 00000000, 00000000, 00000000, 00001001
You use memcpy replication by the byte 8 bits operation: 00000000, and then, it is easy to have all sorts of problems,
Mem at the beginning of the function, such as memcpy, memset, etc., generally used for string manipulation,
But to play string with C, such as embedded development, rarely use these functions, not just four five lines of code, write a good, psychological can clear the operation process, also can free control '\ 0',

CodePudding user response:

reference ~ scarecrow ~ 8 floor response:
let me put it this way, you are a bit drilling in the wrong direction, memcpy is not suitable for integer operation,
Int a [0]=9;
Stored in the memory is 32 bits: 00000000, 00000000, 00000000, 00001001
You use memcpy replication by the byte 8 bits operation: 00000000, and then, it is easy to have all sorts of problems,
Mem at the beginning of the function, such as memcpy, memset, etc., generally used for string manipulation,
But to play string with C, such as embedded development, rarely use these functions, not just four five lines of code, write a good, psychological can clear the operation process, also can free control '\ 0',

String that several functions are STR beginning, those who didn't need to pay attention to the problem \ 0, embedded development mem function with too many oh

CodePudding user response:

Fifth floor said well, undefined behavior does not mean that it will go wrong, it is for this reason, you go to see the so-called memcpy source is completely useless, that's just an equivalent code, and the actual code can be compiled, the actual memcpy tend to make full use of the CPU instruction set optimization, such as using the long even __m128 a copy as a unit, then can produce the illusion of "right" copy, but a large amount of data is not line,nullnullnull
  • Related