C language-detailed explanation of memory functions


1. Memcpy usage and simulation implementation

Return type and parameters:

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

1. The function memcpy copies num bytes of data starting from the source location to the memory location pointed to by destination.
2. This function will not stop when encountering ‘\0’.
3. If there is any overlap between source and destination, the copy results are undefined.
4. Note that the unit is bytes
5. Header file: #include<string.h>

memcpy uses:

int main() {
    
    
	int arr[20] = {
    
     0 };//目标数组
	int arr1[] = {
    
     1,2,3,4,5 };//源数组
	memcpy(arr, arr1, 20);//20单位是字节
	for (int i = 0; i < 5; i++) {
    
    //拷贝完打印
		printf("%d ", arr[i]);
	}
	return 0;
}

operation result:
Insert image description here

memcpy simulation implementation

1. We create a function with the same return type and parameters as the memcpy function
2. Because the unit copied by the memcpy function is bytes, we convert the type to (char*) type
3. Copy through loop

Code:

void* mn_memcpy(void* p1, const void* p2, size_t n) {
    
    
	assert(p1);//判断是否为空指针,参数和类型memcpy一样
	assert(p2);
	while (n) {
    
    //控制拷贝多少个字节
		*((char*)p1) = *((char*)p2);//将void*转化为char*(一字节)
			((char*)p1)++; 
			((char*)p2)++;
		n--;//
	}
}
int main() {
    
    
	int arr[20] = {
    
     0 };
	int arr1[] = {
    
     1,2,3,4,5 };
	mn_memcpy(arr, arr1, 20);
	for (int i = 0; i < 5; i++) {
    
    //拷贝完打印
		printf("%d ", arr[i]);
	}
	return 0;
}

operation result:
Insert image description here

2. Memmove usage and simulation implementation

Return type and parameters:

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

1. The difference from memcpy is that the source memory block and target memory block processed by the memmove function can overlap.
2. If the source space and target space overlap, you must use the memmove function.
3. The unit is bytes
4. Header file: #include<string.h>

memmove uses:

 int main() {
    
    
	int arr1[] = {
    
     1,2,3,4,5 };
	memmove(arr1+1, arr1, 3*sizeof(int));
	for (int i = 0; i < 5; i++) {
    
    //拷贝完打印
		printf("%d ", arr1[i]);
	}
	return 0;
}

Run result:
Insert image description here
memmove simulation:

1. We create a function with the same return type and parameters as the memmove function
2. Because the unit copied by the memmove function is bytes, we convert the type to (char*) type
3. We need to consider three situations and then copy them through a loop

three conditions:
Insert image description here

Code:

void* mn_memmove(void* p1, const void* p2, size_t n) {
    
    
	assert(p1);//判断是否为NULL
	assert(p2);
	if (p1 <= p2 || (char*)p1 >= (char*)p2 + n)//包含两种情况,直接按顺序拷贝
	{
    
    
		while (n) {
    
    
			*((char*)p1) = *((char*)p2);//将void*转化为char*(一字节)
						((char*)p1)++; //(char*)p1加加,字节加一
						((char*)p2)++;
					n--;
		}
	}
	else {
    
    //第三种情况 倒序拷贝
		p1 = (char*)p1 + n - 1;
		p2 = (char*)p2 + n - 1;
		while (n) {
    
    
			*((char*)p1) = *((char*)p2);
			((char*)p1)--;
			((char*)p2)--;
			n--;

		}
	}
}
int main() {
    
    
	int arr1[] = {
    
     1,2,3,4,5,6,7,8,9,0 };
	mn_memmove(arr1+1, arr1, 3*sizeof(int));
	for (int i = 0; i < 10; i++) {
    
    //拷贝完打印
		printf("%d ", arr1[i]);
	}
	return 0;}

operation result:
Insert image description here

3. Use of memset function

Return type and parameters

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

1.memset is used to set the memory and set the value in the memory to the desired content in bytes.
2. Header file: #include<string.h>

use:

int main() {
    
    
	char arr[] = "qwert";
	memset(arr, '0', 3 * sizeof(char));
	printf("%s", arr);
	return 0;
}

operation result:
Insert image description here

4. Use of memcmp function

Return type and parameters:

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

1. Compare num bytes backward starting from the positions pointed to by ptr1 and ptr2 pointers
2. Header file: #include<string.h>

Reply:
Insert image description here
Use:

int main() {
    
    
	char arr[] = "qwert";
	char arr1[] = "abcdf";
	if (memcmp(arr, arr1))
		printf("arr>arr1");
	else
		printf("arr1>arr");
	return 0;
}

operation result:
Insert image description here

The above is what I shared. If there are any mistakes, please leave a message in the comment area.
Finally, thank you everyone for watching!

Guess you like

Origin blog.csdn.net/2302_79539362/article/details/134626090