Explanation and simulation implementation of memory functions memcpy and memmove

Table of Contents

1. What is memcpy function

Simulation ideas

Code

2. What is the memmove function?

Simulation ideas

Code


1. What is the memcpy function

Let’s open the cplusplus official website to view:

memcpy – C++ Reference (cplusplus.com)

The official text shows:

Memory copy block

  • Copies num bytes of value from the location pointed to by the source directly to the memory block pointed to by the destination
  • The underlying types of the objects pointed to by the source and destination pointers are irrelevant to this function; the result is a binary copy of the data
  • This function does not check if there are any terminating null characters in the source – it always copies exactly num bytes
  • To avoid overflow, the size of the arrays pointed to by the destination parameter and the source parameter should be at least num bytes and should not overlap (memmove is a safer approach for overlapping memory blocks)

The above text may not be intuitive enough. We use the official examples to observe its specific role:

/* memcpy example */
#include <stdio.h>
#include <string.h>

struct
{
char name[40];
int age;
} person, person_copy;

int main()
{
char myname[] = "Pierre de Fermat";

/* using memcpy to copy string: */
memcpy(person.name, myname, strlen(myname) + 1);
person.age = 46;

/* using memcpy to copy structure: */
memcpy( & amp;person_copy, & amp;person, sizeof(person));

printf("person_copy: %s, %d \\
", person_copy.name, person_copy.age);

return 0;
}

Output result:

Combining the code and output results, we declared two structures, and we manually assigned values to the two variables of the structure person , and the structure person_copy we Without initializing it, we used the memcpy function to copy the value in the structure person to the structure person_copy , and then we printed Output the values of the variables in person_copy and find that we indeed got these two values.

Simulation Idea

When we get a string, we must first determine the starting point of the copy and the starting point of the copy target.

Then copy the bytes one by one

Code Implementation

Here, we use void* pointers to get the addresses of various data types, and then force conversion to char* to facilitate our access to a single byte (character pointer dereference can only access one byte), in terms of function parameters , you only need to get the two addresses plus the value of how many bytes need to be copied.

void* my_memcpy(void* dest, const void* src, size_t sz)
{
void* ret = dest;
assert(dest & amp; & amp; src);
while (sz--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}

2. What is the memmove function

Let’s open the cplusplus official website to view:

memmove – C++ Reference (cplusplus.com)

The official text shows:

Move memory blocks

  • Copies a value of num bytes from the location pointed to by source to the memory block pointed to by destination. Copying proceeds as if an intermediate buffer was used, allowing destination and source to overlap
  • The underlying types of the objects pointed to by the source and destination pointers are irrelevant to this function; the result is a binary copy of the data
  • This function does not check if there are any terminating null characters in the source – it always copies exactly num bytes
  • To avoid overflow, the size of the arrays pointed to by the destination and source parameters must be at least num bytes.

The above text may not be intuitive enough. We use the official examples to observe its specific role:

/* memmove example */
#include <stdio.h>
#include <string.h>

int main()
{
  char str[] = "memmove can be very useful......";
  memmove (str + 20,str + 15,11);
  puts (str);

  return 0;
}

Output result:

We can see that the original string is memmove can be very useful… but the output is memmove can be very useful. that is, we >Moved the second half of “very useful” some distance back

Simulation ideas

Before simulating the implementation of this function, it is worth noting that compared with the previous memcpy function, the memmove function here has internal repetitive parts when moving. If it is still implemented according to the above method, there will be a situation where the data is repeatedly overwrittenresulting in the discarding of the real data, as shown in the figure.

For this situation, we can solve it by changing the order of swapping bytes, as shown in the figure

In other words, we only need to make reasonable judgments and choose the correct order to implement the function. According to our experience, if what we want to achieve is to move the copy from front to back, then we will move it from back to back. Access from front to back. On the contrary, if we want to move the copy from back to front, then we will access from front to back

Code Implementation

void* my_memmove(void* dest, const void* src, size_t sz)
{
assert(dest & amp; & amp; src);
void* ret = dest;
if (dest < src)
{
//Front->Back
int i = 0;
for (i = 0; i < sz; i + + )
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{
//Back->Front
while (sz--)
{
*((char*)dest + sz) = *((char*)src + sz);
}
}
return ret;
}

That’s it for this sharing. If there are any mistakes, please point them out. I hope my sharing can be helpful to you