Memory functions and their analog implementation

When the body can't bear it, the will will take you out of the tight encirclement

Article directory

1. The memcpy function

Function introduction

Simulation implementation

Two, memmove function

Function introduction

Simulation implementation

Three, memset function

Function introduction

Simulation implementation


Hello everyone, I am Ji Ning. This article introduces the common memory processing functions in C language. 

1. The memcpy function

Function introduction

  memcpy is a memory copy function, which can copy the address of one piece of memory to another piece of memory in units of bytes. Because copying is performed in units of bytes, any data type can be copied, but the two addresses of the memcpy function cannot Overlap, once overlapping, there may be some unpredictable results.

  The first parameter of the memcpy function is the first address of the target memory, the second parameter is the first address of the memory to be copied, and the third parameter is the number of bytes to be copied, because the content in the memory space of the copy source will not change , so add const modification to improve the security of the function.

Simulation implementation

  When simulating the implementation of the memcpy function, you only need to swap the bytes at the corresponding positions, because the parameter passed is a null pointer, so you must first force the type to be converted to char* type, and then dereference and assign.

#include<assert.h>
char* my_memcpy(void* dest,const void* src, size_t num)
{
	assert(dest && src);
	char* ret = dest;
	while (num--)
	{
		*(char*)dest = *(char*)src;
		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}
	return ret;
}

int main()
{
	int arr1[10] = { 0,1,2,3,4,5,6,7,8,9 };
	int arr2[5] = { 11,12,13,14,15 };
	char*st=my_memcpy(arr1, arr2, 20);
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}
	return 0;
}

  In this code, the memory of arr1 and arr2 exists independently, and their memory does not overlap. However, when changing arr1 to arr1+3, and changing arr2 to arr1, the expected result should be {0 1 2 0 1 2 3 4 8 9}, but actually becomes {0 1 2 0 1 2 0 1 8 9} during the operation. This is why the memory of their two spaces overlaps.

  To solve this problem, most of us will use another function memmove.

Two, memmove function

Function introduction

  The memmove function can move the memory of one piece of memory to another. It is also similar to the function of the copy function (memcpy), but it has more memory copy functions that can handle overlapping memory spaces than the memcpy function.

Its function is the same as the ideal strcpy function for copying memory space, so I won’t go into details 

Simulation implementation

  Through a simple test, it can be obtained that when the space of the target memory is behind the space of the source memory, the purpose can be achieved by copying from the initial position one by one by the above method of implementing the strcpy function, as shown in the figure

  If the target memory space is in front of the source memory space, the desired result cannot be achieved

  Therefore, for the case where the target memory space is in front of the source memory space, we can copy from the back of the memory space forward,

  To sum up, when the target space is in front of the source space, copy from back to front; when the target space is in front of the source space, copy from front to back. Use the if...else statement in the function to control it.

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

Three, memset function

Function introduction

  The role of the memset function is to change the data in bytes.

void* my_memset(void* ptr, int value, int num)
{
	int i = 0;
	for (i = 0; i < num; i++)
	{
		*((char*)ptr+i) = value;
	}
	return (char*)ptr;
}
int main()
{
	int arr[10] = { 0 };
	my_memset(arr, 'x', 39);
	printf("%s",(char*)arr);
	return 0;
}

   The first parameter of the memest function is the first address of the data to be changed, the second parameter is the content to be changed (shaping family), and the third parameter is the number of bytes to be changed.

  Although arr is an integer array, the memset function can change the data of each byte into character data in units of bytes, so the second parameter of the memset function only needs to be an integer family.

Simulation implementation

void* my_memset(void* ptr, int value, int num)
{
	int i = 0;
	for (i = 0; i < num; i++)
	{
		*((char*)ptr+i) = value;
	}
	return (char*)ptr;
}
int main()
{
	int arr[10] = { 0 };
	my_memset(arr, 'x', 39);
	printf("%s",(char*)arr);
	return 0;
}

insert image description here

  The blogger has been writing for a long time, if you can give the blogger a free triple combo to encourage the blogger, then I think your Thai pants are really spicy! ! !

Guess you like

Origin blog.csdn.net/zyb___/article/details/131858447