Functions and differences of memcpy, memmove, memcmp, and memset functions

1. memcpy and memmove

1、memcpy

Function: Copy num bytes of data backward from the location of source to the memory location of destination.

Notice:

  • The memcpy() function will not stop when it encounters '\0' (the strcpy string copy function will stop when it encounters '\0');
  • The content pointed to by destination and source cannot overlap, otherwise the desired result cannot be obtained.

void* memcpy(void* destination,const void* source,size_t num);
2、memmove

2、memmove

Function: Copy num bytes of data backward from the position of source and move it to the memory location of destination.

Note: The difference from the memcpy() function is that the original memory block and the target memory block processed by the memmove() function can overlap.

void* memmove(void* destination,const void* source,size_t num);

3. The difference between memove and memcpy and the problem of overlapping memory

memmove is an upgraded version of memcpy , memmove is more secure.

For details, see: [C Language] Talking about the difference between memcpy and memmove_DanteIoVeYou's Blog-CSDN Blog


Two, memcmp

Function: Used to compare the contents of two memory areas.

int memcmp(const void * Buf1,const void * Buf2,size_t Size);

buf1: Pointer to the first memory area.
buf2: pointer to the second memory area.
size: The number of bytes to compare.

  • If the return value < 0, it means that str1 is smaller than str2.
  • If the return value > 0, it means that str1 is greater than str2.
  • If the return value = 0, it means that str1 is equal to str2.

 


3. memset

Function: memory assignment function, used to assign a certain memory space; included in the < string.h > header file.
Detailed explanation : Set the value of the first n bytes of the allocated memory space s to the value c.
Note : This function assigns values ​​to arrays or structures byte by byte.

void *memset(void *s, int v, size_t n)

s  is the name of the array, or a pointer to an internal space;
v  is the value to be filled;
n  is the number of bytes to be filled;

If it is a character type array, memset can be used casually, but for an int type array, it is generally only used to clear 0 or fill -1, if it is filled with other data, an error will occur.


4. Reference content

[C language] Talking about the difference between memcpy and memmove_DanteIoVeYou's Blog-CSDN Blog

C language memcpy, memmove, memcmp, memset functions_sheygshsi's Blog-CSDN Blog

Introduction to C/C++---memset, memcpy, memcmp functions_c/c++ memcpy with different array sizes 

Guess you like

Origin blog.csdn.net/qq_41709234/article/details/132134454