Do you really understand memory functions (memcpy, memmove, memset, memcmp)?

  • Blog homepage: Jiang Chijun’s blog
  • ?Inclusion column: The road to advanced C language
  • Column recommendation: ?Beginner’s road to C language ?Exploration of data structure
  • Code repository: Jiang Chijun’s code repository
  • Everyone is welcome to likeCommentCollect?

Article directory

  • 1.memcpy function
    • 1.1 [Notes]:
    • 1.2【Example】
    • Simulation implementation of memcpy
  • 2.memmove function
    • 2.1 [Notes]:
    • 2.2【Example】
    • Simulation implementation of memmove
  • 3.memset function
    • 3.1 [Notes]:
    • 3.2【Example】
  • 4.memcmp function
    • 4.1 [Notes]:
    • 4.2【Example】

Foreword

When we talk about memory functions, what are we talking about? Simply put, memory functions are functions used to process and operate on memory. These functions allocate, free, and manipulate memory while the program is running and are critical to the program’s performance, efficiency, and stability. In the development of programming languages, memory functions have always played an important role, especially in modern programming, their significance is even more prominent.
This article will delve into the knowledge of memory functions, aiming to help readers better understand this area and improve their skills in modern programming.

Header file

1.memcpy function

Statement

void *memcpy(void *dest, const void *src, size_t num);

Parameters

  • dest – – – Pointer to the destination array used to store the copied content, type cast to a void* pointer.
  • src – – – Points to the data source to be copied, type cast to void* pointer.
  • num – – – The number of bytes to be copied.

Return value

  • This function returns a pointer to the destination memory area dest.

1.1 [Notes]:

  • C library function void *memcpy(void *dest, const void *src, size_t num) copies num bytes from storage area src to Storage area dest.
  • The function memcpy copies num bytes of data backward starting from the location of src to the memory location of dest.
  • This function does not stop when encountering '\0'.
  • If there is any overlap between src and dest, the results of the copy are undefined.

1.2【Example】

/* 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;
}

Simulation implementation of memcpy

//Simulating implementation of memcpy
#include<stdio.h>
#include<assert.h>

 //Copying without overlapping memory can use memcpy
void* my_memcpy(void* dest,const void* src, size_t sz)
{<!-- -->
assert(dest & amp; & amp; src);
void* ret = dest;
while (sz--)
{<!-- -->
//Because we don’t know the data type to be copied to, we copy it byte by byte.
*(char*)dest = *(char*)src;
//Forced type conversion is temporary and cannot be written as (char*)dest + +, and + + has a higher priority, and void* type pointers cannot be added or subtracted.
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return dest;
}

int main()
{<!-- -->
int arr1[10] = {<!-- --> 0 };
int arr2[] = {<!-- --> 1,2,3,4,5 };
my_memcpy(arr1, arr2, 20);
for (int i = 0; i < 5; i + + )
{<!-- -->
printf("%d ", arr1[i]); //The result is 1 2 3 4 5
}
printf("\\
");
}

2.memmove function

Statement

void *memmove(void *dest, const void *src, size_t num);

Parameters

  • dest – – – Pointer to the destination array used to store the copied content, type cast to a void* pointer.
  • src – – – Points to the data source to be copied, type cast to void* pointer.
  • num – – – The number of bytes to be copied.

Return value

  • This function returns a pointer to the destination memory area dest.

2.1【Notes】:

  • C library function void *memmove(void *dest, const void *src, size_t num) copies num characters from src to dest, but memmove() is a safer method than memcpy() when it comes to overlapping memory blocks. If the target area and the source area overlap, memmove() can ensure that the bytes of the overlapping area are copied to the target area before the source string is overwritten. The content of the source area will be changed after copying. If the target area does not overlap with the source area, it has the same function as the memcpy() function.

2.2【Example】

/* 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;
}

Simulation implementation of memmove

//Simulate implementation of memmove
#include<stdio.h>
#include<assert.h>

//Copy of overlapping memory, use memmove
void* my_memmove(void* dest, void* src, size_t sz)
{<!-- -->
assert(dest & amp; & amp; src);
void* ret = dest;
if (dest < src)
{<!-- -->
//before -> after
while (sz--)
{<!-- -->
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
/*for (int 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;
}

int main()
{<!-- -->
int arr[] = {<!-- --> 1,2,3,4,5,6,7,8,9,10};
my_memmove(arr, arr + 2, 20);
for (int i = 0; i < 10; i + + )
{<!-- -->
printf("%d ", arr[i]);
}
printf("\\
");
}

3.memset function

Statement

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

Parameters

  • ptr – – – Points to the memory block to be filled.
  • value – – – The value to be set. The value is passed as int, but the function uses the unsigned character form of the value when filling the memory block.
  • num – – – The number of characters to be set to this value.

Return value

  • This value returns a pointer to the storage area ptr.

3.1【Notes】:

-C library function void *memset(void *ptr, int value, size_t num) copies the character value (an unsigned character) to the parameter ptr The first num characters of the string pointed to.

3.2【Example】

#include <stdio.h>
#include <string.h>
 
int main()
{<!-- -->
   char str[50];
 
   strcpy(str,"This is string.h library function");
   puts(str);
 
   memset(str,'$',7);
   puts(str);
   
   return(0);
}

4.memcmp function

Statement

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

Parameters

  • ptr1 – – – Pointer to a memory block.
  • ptr2 – – – Pointer to a memory block.
  • num – – – The number of bytes to be compared.

Return value

  • If the return value is < 0, it means that ptr1 is less than ptr2.
  • If the return value is > 0, it means that ptr1 is greater than ptr2.
  • If the return value = 0, it means that ptr1 is equal to ptr2.

4.1 [Notes]:

  • The C library function int memcmp(const void *ptr1, const void *ptr2, size_t num)) combines the storage area ptr1 and the storage area ptr2 The first num bytes are compared.

4.2【Example】

Code 1:

/* memcmp example */
#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;
}

Code 2:

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

int main()
{<!-- -->
char str1[15];
char str2[15];
int ret;

memcpy(str1, "abcdef", 6);
memcpy(str2, "ABCDEF", 6);

ret = memcmp(str1, str2, 5);

if (ret > 0)
{<!-- -->
printf("str1 is greater than str2\\
");
}
else if (ret < 0)
{<!-- -->
printf("str1 is less than str2\\
");
}
else
{<!-- -->
printf("str1 equals str2\\
");
}

return(0);
}

That’s it for today’s sharing. If you think the blogger’s article is good, please support the blogger three times in a row