Memory functions–hand-tear memove/memcpy and other functions, easy to understand!

Table of Contents

1. Usage and simulation implementation of memcpy function

Manual memcpy function

2. Usage and simulation implementation of memmove function

Hand-tear memmove function

3. Use of memset function

4. Use of memcmp function


This is a screenshot of an interview. The two functions mentioned are the two functions of the memory functions we will learn in this chapter. It can be seen that memory functions are still relatively important. Next, I will help you easily understand all memory functions in C language, show their internal source code, and help you learn about memory functions in interviews! Like + favorite and let’s get started.

1. The use and simulation implementation of the memcpy function

void * memcpy ( void * destination, const void * source, size_t num );
    
//destination: the starting address of the destination memory

//source: the starting address of the source address memory

//num: The number of bytes to be copied

Let’s pave the way, what is void*? It is a generic type and can accept pointers of any data type, but it must be forced when used.

It’s obvious why void* is used here. If you don’t know what data type you want to accept, then use generics, which can accept all data types.

So what does const mean? Obviously we are putting the source address content into the destination memory. The source memory does not need to be modified, so we use const to modify it to avoid misoperation.

#include <stdio.h>
#include <string.h>
int main()
{
int arr1[10] = { 0 };
int arr2[] = { 1,2,3,4,5,6,7,8,9,10 };
memcpy(arr1, arr2, sizeof(arr2));
size_t sz = sizeof(arr2) / sizeof(arr2[0]);

printf("arr2[] = ");
for (size_t i = 0;i < sz;i + + )
{
printf("%d ", arr1[i]);
}
printf("\
");

char c1[6] = { 0 };
char c2[] = "hello";
memcpy(c1,c2,sizeof(c2));
printf("c1[] = %s",c1);
return 0;
}

Hand-shred memcpy function

void* my_memcpy(void* dest, void* src, size_t num)
{
    assert(dest & amp; & amp; src);
void* ret = dest;
while(num) //size_t type has no negative numbers! ! !
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1; //(char*)dezst + + program error
src = (char*)src + 1;
num--;
}
return ret;
}

Here we have a question, can we copy the contents of different locations in the same memory space? For example, put int arr[10] ={1,2,3,4,5,6,7,8,9,10}, put 1,2,3,4,5 into 3,4,5,6 , what about the position of 7?

int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };

Obviously this is not the result we want. Why? This is because the memory addresses overlap. The memmove function we implemented copies from front to back. 3, 4, and 5 have long been copied to 1, 2. How can it be copied from back to front? Woolen cloth. So it can be concluded that the memcpy function can handle data copying between non-overlapping memories.

Then if we want to copy data from overlapping memory, we need to use the memmove function.

2. The use and simulation implementation of the memmove function

void * memmove ( void * destination, const void * source, size_t num );
    
//destination: the starting address of the destination memory

//source: the starting address of the source address memory

//num: The number of bytes to be copied

Let’s use the memmove function to see the overlapping memory copy we talked about above.

In fact, it is easy for us to figure out the problem. Since there is a problem with copying from front to back, what about copying from back to front? In this way, it will not be overwritten. It’s really that simple.

However, we should note that not all scenes are from front to back, and not all scenes are from back to front.

We need to use it flexibly according to different scenarios. This is how the memmove function is implemented, and it will make judgments.

Hand-shred memmove function

void* my_memmove(void* dest, const void* src, size_t num)
{
assert(dest & amp; & amp; src);
void* ret = dest;
if (dest > src)
{
while (num--)
{
*((char*)dest + num) = *((char*)src + num);
}
}
else
{
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
        }
}
return ret;
}

3. Use of memset function

void * memset ( void * ptr, int value, size_t num );

The memset function is used to set the memory and set the value in the memory to the desired content in bytes.

#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "xxxxxxxxxx";
memset(arr, 'h', 5);
printf("%s", arr);
}

4. Usage of memcmp function

 int memcmp ( const void * ptr1, const void * ptr2, size_t num );

Compare num bytes backward starting from the positions pointed to by ptr1 and ptr2 pointers

The return value is as follows:

#include <stdio.h>
#include <string.h>

int main()
{
  char buffer1[] = "DWgaOtP12df0";
  char buffer2[] = "DWGAOTP12DF0";

  int n;

  n=memcmp (buffer1, buffer2, sizeof(buffer1));

  if (n>0) printf ("'%s' is greater than '%s'.\
",buffer1,buffer2);
  else if (n<0) printf ("'%s' is less than '%s'.\
",buffer1,buffer2);
  else printf ("'%s' is the same as '%s'.\
",buffer1,buffer2);

  return 0;
}