The purpose of memcpy and Memmove is to copy the contents of the source memory address of n bytes to the destination memory address.
However, when the source memory and target memory overlap, memcpy will have an error, and Memmove can implement the copy correctly, but this also adds a little overhead.
Memmove Treatment measures:
(1) No copy is made when the first address of the source memory equals the first address of the target memory
(2) A forward copy is applied when the first address of the source memory is greater than the destination memory's first address
(3) Reverse copy is applied when the first address of the source memory is less than the first address of the target memory
--memcpy implementation
1 |
void* memcpy(void* dest, constvoid* src, size_tn) |
3 |
char* d = (char*) dest; |
4 |
constchar* s = (constchar*) src; |
Notes:
- The memcpy parameter pointer type is void*, and the specific assignment operation is in bytes.
- Type conversions are required.
- The return is also the void* type of dest.
--Memmove implementation
01 |
void* memmove(void* dest, constvoid* src, size_tn) |
03 |
char* d = (char*) dest; |
04 |
constchar* s = (constchar*) src; |
08 |
// start at beginning of s |
:
(1)内存低端 <-----s-----> <-----d-----> 内存高端 start at end of s
(2)内存低端 <-----s--<==>--d-----> 内存高端 start at end of s
(3)内存低端 <-----sd-----> 内存高端 do nothing
(4)内存低端 <-----d--<==>--s-----> 内存高端 start at beginning of s
(5)内存低端 <-----d-----> <-----s-----> 内存高端 start at beginning of s
Notes:
- When S==d, nothing is done.
- D in front, forward copy.
- D in the back, reverse copy.
1.memmove
Function prototypes: void *memmove (void *dest, const void *source, size_t count)
Return value Description: Returns the void * pointer to dest
Parameter description: Dest,source is the destination string and the first address of the source string, respectively. Count is the number of characters to move
Function Description: Memmove is used to copy count characters from source to dest, and if the target region and source area overlap, memmove can ensure that the source string copies the bytes of the overlapping region to the target area before being overwritten.
2.memcpy
Function prototypes: void *memcpy (void *dest, const void *source, size_t count);
Return value Description: Returns the void * pointer to dest
Function Description: The memcpy function and Memmove are the same, but the regions in dest and source in memcpy cannot overlap, otherwise unknown results occur.
Prototype: extern char *strcpy (char *dest,char *src); function: Copy the null-terminated string from SRC to the array referred to by Dest. Description: The memory areas referred to by SRC and dest cannot overlap and dest must have enough space to accommodate the SRC string, returning a pointer to Dest.
In fact, the implementation of strcpy more, but the idea is consistent, generally with C to achieve, but memcpy and memmove such functions may be implemented with the Assembly, and make full use of the idea of block copy, will not single-byte single-byte copy. So efficiency strcpy<memcpy.
Memmove generally because to determine whether the memory is coincident, the efficiency will be lower than memcpy.
1/***
2 * @brief copied directly in bytes
3 * The implementation in the library is implemented as a compilation,
4 * You can actually call the Strncat function directly
5 * **/
6 void *memcpy (void *dst,void *src,size_t N)
7 {
8 Char *DP = (char *) DST;
9 Char *sp = (char *) src;
ASSERT (Src!=0 && (dst!=0) && (n>0));//not null
One while (n--)
* (dp++) = * (sp++);
13/**! Border */
DP = ' + ';
return DST;
16}
Memmove
1 void *memmove (void *dst,const void *src,int n)
2 {
3 Char *DP = (char *) DST;
4 Char *sp = (char *) src;
5 assert ((src!=0) && (dst!=0) && (n>0));//not null
6//non-overlapping
7//DP < SP
8//DP > (sp+n)
9 If (dp<sp| | (sp+n) >=DP)
10 {
One while (n--)
* (dp++) = * (sp++);
*DP = ' + ';
}else if (SP<DP)//overlap (At this time the condition sp<dp< (sp+n)) If the SP==DP is quickly returned
15 {//Reverse copy
SP + = n;
+ DP + = n;
*DP = ' + ';
while (n--)
* (--DP) = * (--SP);
21}
return DST;
23}
Note For coincident to reverse copy
/*
*magicman
*mymemcpy.c
* Do not call library functions to implement memory copy
*/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void *mymemcpy (void *dest, const void *src, int len)
{
ASSERT ((dest! = null) && (src! = null) && (len >= 0));
if (dest = = src)
{
return dest;
}
while (len--> 0)
{
* (char *) dest++ = * (char *) src++;
}
return dest;
}
int main (int argc, char argv[])
{
Char str[20] = "Testing mymemory!";
Char pstr[20] = "";
char *pp = str;
int ia[10] = {1,2,3,4,5,6,7,8,9,10};
int ib[10] = {};
int *ip = NULL;
mymemcpy (void *) PSTR, (void *) str, sizeof (str));
printf ("%s/n", pstr);
printf ("%s/n", mymemcpy (void *) pp, (void *) str, 20));
mymemcpy (void *) IB, (void *) IA, 5*sizeof (int));
for (IP = IB; IP < IB + ten; ip++)
{
printf ("%d", *IP);
}
printf ("/n");
return 0;
}
Let yourself implement the memcpy library function, which requires consideration of special circumstances, the presence of two memory overlays, and the case where the pointer is empty.
Some conclusions:
The SRC source data should be retained by the 1,memcpy implementation to copy n bytes from source sources to the target Destin.
2,memmove implementation moves a byte, src source data can not be preserved.
3,memcpy does not consider the memory overwrite problem (known by the Assert condition), and Memmove considers the memory cover problem, and gives the solution.
4,memcpy and memmove do not need to consider the array cross-border problem, the length of DST should be greater than the length of SRC, this is the caller should consider the issue.
C + + memcpy and Memmove