Detailed explanation of C/C++ character functions and string functions——Detailed explanation and simulation of memory functions

Personal homepage : Click me to enter the homepage

Column classification: C language elementary       C language programming——KTV        C language mini-game      C language advanced

C language test questions

Everyone is welcome to like, comment and collect.

Work hard together and go to a big factory together.

Table of contents

1 Introduction

2.memcpy function

3.memmove function

4.memset function

 5.memcmp function


1 Introduction

           We have previously learned about string functions with limited length and string functions with unlimited length. Among them, strcmp corresponds to strncmp function, strcpy function corresponds to strncpy function, and strcat function corresponds to strncat function. Today we mainly understand the four memory functions. They are memcpy function, memmove function, memset function, and memcmp function. The functions of memcpy function and strcpy function are similar, and the memcmp function is similar to strcmp function. Next, let us feel the charm of these functions.

2.memcpy function

        Many people are exposed to the memcpy function for the first time. We go to the cplusplus website cplusplus to view the parameters of the memcpy function.

        We see that the parameters of the function are void * destination, const void * source, size_t num. For the parameters, we need to know what function this function has. It mainly copies any type of data, so destination and source are both void* type, because the void* type can hold any type of pointer, where destination is the pointer to the target, source is the pointer to the source, and num is the number of bytes you want to copy. Next, the code demonstration is as follows:

#include<stdio.h>
#include <string.h>
int main()
{
	int arr1[10] = { 0 };
	int arr2[5] = { 2,3,4,5,6 };
	memcpy(arr1, arr2, 20);
	int i;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}
	return 0;
}

 When we run the code, the results are as follows:

Next, simulate the function

#include<stdio.h>
#include <string.h>
void* my_memcpy(void* str1, const void* str2, size_t num)
{
	char* p = (char*)str1;
	while (num--)
	{
		*(char*)str1 = *(char*)str2;
		str1 = (char*)str1 + 1;
		str2 = (char*)str2 + 1;
	}
	return p;
}
int main()
{
	int arr1[10] = { 0 };
	int arr2[5] = { 2,3,4,5,6 };
	my_memcpy(arr1, arr2, 20);
	int i;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}
	return 0;
}

        Here we need to perform forced conversion, convert to char* type and then modify it byte by byte, but this function has certain limitations. When its destination pointer and source pointer point to the same data and there is overlap, the function There will be different results than expected. We can simply draw it:

When we want to copy str1 to str2, when we copy the first three data, str1 coincides with the original str2

         If you want to modify it again, duplicate copies will occur. According to the standard memcpy function of the C language, it copies data from different sources. However, for data from the same array, another function is needed. This function is the memmove function. Next, we introduce memmove. function.

3.memmove function

        We also go to the cplusplus website to view the parameters and functions of the memmove function.

           We see that the parameters of the function are void * destination, const void * source, size_t num. The function of the memmove function is similar to the function of the memcpy function, except that memmove copies data from the same array. Both destination and source are void* type. , because the void* type can hold any type of pointer, where destination is the pointer to the target, source is the pointer to the source, and num is the number of bytes you want to copy. Next, the code demonstration is as follows:

#include <stdio.h>
#include <string.h>
int main()
{
	int arr[10] = { 0,1,2,3,4,5,6,7,8,9};
	memmove(arr, arr + 3, 20);
	int i;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;

}

The running results are as follows:

Next, we carry out the simulation implementation of the memmove function. The code is as follows:

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

}

 Here we need to look at two situations, the first is str1 in front of str2

In this case we only need to assign values ​​from front to back. For the second kind

 We cannot copy str1 from front to back after str2, otherwise there will be repeated assignments and errors. In this case, we only need to assign values ​​from back to front to solve the problem.

4.memset function

         Let’s go to the cplusplus website to check

        The memset function is a function that initializes data. This data can be of any type, but strings are more suitable. ptr points to the location you want to modify, value refers to the value you want to modify, and num refers to how many bytes to modify. We directly Enter the code demonstration, the code is as follows:

#include <stdio.h>
#include<string.h>
int main()
{
	char arr[] = "abcdefg";
	memset(arr, 'x', 4);
	printf("%s", arr);
	return 0;
}

The running results are as follows:

Next, enter our simulation implementation. The code is as follows:

#include <stdio.h>
#include<string.h>
#include <assert.h>
void* my_memset(void* str, int vaul, size_t sz)
{
	assert(str);
	char* p = (char*)str;
	while (sz--)
	{
		*(char*)str = vaul;
		str = (char*)str + 1;
	}
	return p;
}
int main()
{
	char arr[] = "abcdefg";
	my_memset(arr, 'x', 4);
	printf("%s", arr);
	return 0;
}

 5.memcmp function

        We enter the cplusplus website to view the parameters of the function 

        The function of the memcmp function is to compare the size of data, where num is the number of bytes of data to be compared. The function is similar to strncmp. Let’s go directly to the code:

 

#include <stdio.h>
#include <string.h>
int main()
{
	int arr[5] = { 0,1,2,3,4 };
	int arr1[5] = { 0 };
	int ret = memcmp(arr, arr1, 5);
	printf("%d", ret);
	return 0;
}

We can understand it as 

The first 4 bytes are the same, and the fifth byte str1 is greater than str2, so 1 is returned. The running results are as follows;

 

Next, simulate the implementation of the memcmp function. The code is as follows:

#include <stdio.h>
#include <string.h>
#include <assert.h>
int my_memcmp(const void* str1, const void* str2, size_t sz)
{
	assert(str1 && str2);
	int i;
	for (i = 0; i < sz; i++)
	{
		if (*(char*)str1 >*(char*)str2)
		{
			return 1;
		}
		else if (*(char*)str1 < *(char*)str2)
		{
			return -1;
		}
		str1 = (char*)str1 + 1;
		str2 = (char*)str2 + 1;
	}
	return 0;
}
int main()
{
	int arr[5] = { 0,1,2,3,4 };
	int arr1[5] = { 0 };
	int ret = my_memcmp(arr, arr1, 5);
	printf("%d", ret);
	return 0;
}

That’s the end of today’s content, I hope you can learn something.

Guess you like

Origin blog.csdn.net/Infernal_Puppet/article/details/133560166