Memory functions: memcpy/memmove/memcmp/memset

 memcpy


    //void* memcpy(void* destination, const void* source, size_t num);
        //void* can receive any type of pointer, and forced type conversion must be performed before operation or dereference; destination is the starting position of the target space; source is the starting position of the source space; the unit of num is bytes
        //The return value of memcpy is the starting position of the target space, that is, destination
        //That is, memcpy copies the first num bytes of the source space to the target space
    / / memcpy is a memory copy function, memory is not a literal meaning, it refers to any type of data stored in memory
    // strcpy can only copy strings, while memcpy can copy any type of data
    // (the function needs to include a header file < string.h>)
    //memcpy is not suitable for situations where destination and source have memory overlap. This situation is specially handled by the mememove function mentioned next.

int main()
{
	int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[] = { 2,4,6,8,10 };
	memcpy(arr1, arr2, 20);	//20的单位是字节
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);	//2 4 6 8 10 6 7 8 9 10
	}
	return 0;
}

         Simulate implementation of memcpy

 

void* my_memcpy(void* arr1, const void* arr2, size_t num)
{
	void* ret = arr1;
	while (num--)
	{
		*(char*)arr1 = *(char*)arr2;//强制类型转换只是在该语句中,该语句执行完又会恢复到原来的类型,即强制类型转换都是临时性的
		((char*)arr1)++;
		((char*)arr2)++;
	}
	return ret;
}
int main()
{
	int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[] = { 2,4,6,8,10 };
	my_memcpy(arr1, arr2, 20);	//20的单位是字节
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);	//2 4 6 8 10 6 7 8 9 10
	}
	return 0;
}

 

 memmove


    //Compared to memcpy, memmove specifically solves the memory overlap problem between the target space and the source space
    //(The function needs to include the header file <string.h>)

int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	memmove(arr + , arr + 2, 240);
		//arr是数组名,表示数组首元素地址;arr + 4是数组第五个元素地址;arr + 2是数组第三个元素地址;20的单位是字节(即5个int类型数据)
		//arr + 4是目标空间的起始地址,arr + 2是源空间的起始地址,即从arr + 2的位置开始向后复制20个字节(5个int型)的数据到以arr + 4为起始地址的目标空间
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);	//1 2 3 4 3 4 5 6 7 10
	}
	return 0;
}

         Simulate implementation of memmove


    //If the source space and target space overlap: when the starting position of the target space is greater than the starting position of the source space (that is, the starting position of the target space is a higher address relative to the starting position of the source space), the source space The data stored in the internal memory is copied to the target space in order from high address to low address; in other cases, the data is copied in order from low address to high address.

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

 

 memcmp


    //int memcmp(const void* ptr1, const void* ptr2, size_t num) Note: The unit of num here is bytes
    //memcmp compares the first num bytes starting from the ptr1 and ptr2 pointers
    //memcmp and strcmp The difference is that any type of data can be compared, and everything else is the same

int main()
{
	char arr1[] = "abcddef";
	char arr2[] = "abcdefg";
	int p1 = memcmp(arr1, arr2, 4);
	int p2 = memcmp(arr1, arr2, 5);
	printf("%d\n", p1);		//0
	printf("%d\n", p2);		//-1
	return 0;
}

 

memset


    //void* memset(void* ptr, int value, size_t num)
    //memset sets the value of the first num bytes of the content pointed to by ptr to value. Note: the unit of num is bytes. The return value of memset is ptr. 

int main()
{
	char str[] = "hello world";
	int arr[10] = { 0 };
	memset(str, 'a', 5);
	memset(arr, 1, 10);
	printf("%s\n", str);	//aaaaa world
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);	//16843009 16843009 257 0 0 0 0 0 0 0
			//memset不能实现将arr的所有元素设置成1,主要原因还是memset中的参数num的单位是字节,它是按字节来设置的
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/libj2023/article/details/131692587