[C language] memory operation function


1.memcpy

strcpy can only copy strings, and memcpy can not only copy other types of content.

insert image description here
例子

int main()
{
	int arr1[10] = { 0 };
	int arr2[10] = { 1,2,3,4,5,6,7,8,9,10 };
	memcpy(arr1, arr2, sizeof(int)*5);//可以了解为从arr2开始,拷贝20个字节到arr1指向的空间
	int i = 0;
	for (i = 0; i < 5; i++)
	{
		printf("%d ", *(arr1 + i));
	}
	return 0;
}

Results
1 2 3 4 5

注意
(1) Both the target array and the source array must be large enough.
(2) If this function has not copied num bytes, it will not stop when encountering '\0'.
(3) If you want to start copying from other locations in the source array, you can modify the starting address, such as memcpy(arr1,arr2+4,sizeof(int)*5).


2. Simulate the realization of memcpy

void* my_memcpy(void* dest, const void* sour, int num)
{
	assert(dest && sour);
	void* ret = dest;
	while (num--)
	{
		*(char*)dest = *(char*)sour;
		dest = (char*)dest + 1;
		sour = (char*)sour + 1;
	}
	return ret;
}
int main()
{
	int arr1[10] = { 0 };
	int arr2[10] = { 1,2,3,4,5,6,7,8,9,10 };
	my_memcpy(arr1, arr2, sizeof(int)*5);//可以了解为从arr2开始,拷贝20个字节到arr1指向的空间
	int i = 0;
	for (i = 0; i < 5; i++)
	{
		printf("%d ", *(arr1 + i));
	}
	return 0;
}

Results
1 2 3 4 5

疑惑
(1) What would be the result if you copied it yourself? (that is, dest and sour have overlapping parts) such as memcpy(arr1+2,arr1,20)

There are two possible outcomes:
1 2 1 2 3 4 5 8 9 10
1 2 1 2 1 2 1 8 9 10

(2) Why does the second result appear? When you follow the steps in the figure
insert image description here
(3), such a result is not up to our expectation, how can we get the first result?
In the same array, when sour is on the left of dest, the byte pointed to by sour is copied to the position pointed to by dest from front to back, and overwriting will definitely occur, resulting in duplication. Then we process the source data from back to front; when sour is on the right side of dest, we process the source data from front to back.
(4) The above involves the simulation implementation of memmove, let's first understand memmove


3.memmove

memmove is equivalent to the enhanced version of momcpy, which also copies memory blocks, but it can realize overlapping copies of memory blocks.
例子

int main()
{
	int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[10] = { 0 };
	memmove(arr1 + 2, arr1, 20);
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", *(arr1 + i));
	}
	return 0;
}

Results
1 2 1 2 3 4 5 8 9 10

insert image description here


4. Simulate memmove

void* my_memmove(void* dest, void* sour, int num)
{
	assert(dest && sour);
	void* ret = dest;
	if (sour > dest)
	{
		while (num--)
		{
			*(char*)dest = *(char*)sour;
			sour = (char*)sour + 1;
			dest = (char*)dest + 1;
		}
	}
	else
	{
		while (num--)
		{
			*((char*)dest + num) = *((char*)sour + num);
		}
	}
	return ret;
}
int main()
{
	int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[10] = { 0 };
	my_memmove(arr1 + 2, arr1, 20);
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", *(arr1 + i));
	}
	return 0;
}

Results
1 2 1 2 3 4 5 8 9 10

注意
The array to be copied must have enough space to store the copied content to prevent out-of-bounds access


5.memcmp

insert image description here
The return value of this function is the same as the return value of strcpy. If the byte pointed to by ptr1 is equal to the byte pointed to by ptr2, then the two are ++. If num bytes have already been compared, 0 is returned; if the byte pointed to by ptr1 If the section is larger than the byte pointed to by ptr2, a number greater than 0 is returned; if the byte pointed to by ptr1 is smaller than the byte pointed to by ptr2, a number smaller than 0 is returned.
例子

int main()
{
	int arr1[] = { 1,2,3 };
	int arr2[] = { 1,2,5 };
	printf("%d", memcmp(arr1, arr2,9));//比较9个字节
	//已只数据在内存中的存储方式是小端存储(低位字节序放在低地址处,高位字节序放在高地址处)
	//arr1在内存中的存储:01 00 00 00 02 00 00 00 03 00 00 00
	//arr2在内存中的存储:01 00 00 00 02 00 00 00 05 00 00 00
	//比较前9个字节,到第九个字节时05>03,所以arr1<arr2,返回小于0的数字
	return 0;
}

result
-1


6.memset

insert image description here
例子

int main()
{
	char arr[] = "hello world";
	//将hello改为xxxxx
	memset(arr, 'x', 5);
	printf("%s\n", arr);
	//将world改为yyyyy
	memset(arr + 6, 'y', 5);
	printf("%s\n", arr);
	return 0;
}

Result
xxxxx world
xxxxx yyyyy

注意
(1) memset is processed in bytes
(2) memset is usually used to initialize data

Guess you like

Origin blog.csdn.net/Zhuang_N/article/details/128839890