C language - memory manipulation function

Table of contents

1.memcpy

1.1 Simulation implementation

2.memmove

2.1 Simulation implementation 

3.memcmp

3.1 memcmp use 

4.memset

4.1 memset use 


1.memcpy

  1. Definition: Realize memory copy. (The strcpy function was introduced in my last blog string function, but strcpy can only operate on strings and cannot operate on other types of arrays. And memcpy can copy other types of arrays.)
  2. Note: Data of void* type cannot be dereferenced directly, and must be cast to other types.
  3. Simulation implementation

1.1 Simulation implementation

#include<stdio.h>
#include<assert.h>

void* my_memcpy(void* dest, const void* src, size_t count)
{
	assert(dest && src);
	void* ret = dest;
	while (count--)
	{
		*(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] = { 0 };
	my_memcpy(arr2, arr1, 20);
	int i = 0;
	int sz = sizeof(arr2) / sizeof(arr2[0]);
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;
}
//结果是 0 1 2 3 4
The count in the formal parameter of the my_memcpy function refers to the size of how many bytes to copy.

2.memmove

  1. Definition: Implement overlapping memory copy.

  2. Simulation implementation 

2.1 Simulation implementation 

#include<stdio.h>
#include<assert.h>

void* my_memmove(void* dest, const void* src, size_t count)
{
	assert(dest && src);
	void* ret = dest;
	if (dest > src)//从src的后面开始拷贝
	{
		while (count--)
		{
			*((char*)dest + count) = *((char*)src + count);
		}
	}
	else//从src的前面开始拷贝
	{
		while (count--)
		{
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	return ret;
}

int main()
{
	int arr[10] = { 0,1,2,3,4,5,6,7,8,9 };
	my_memmove(arr+2, arr, 20);

	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}

	return 0;
}
//结果是:0 1 0 1 2 3 4 7 8 9

 Unlike memcpy, if the example of the above code is implemented with memcpy, the result will be 0 1 0 1 0 1 0 7 8 9 . The copied data will be overwritten. And memmove can implement memory overlay copy very well.

3.memcmp

  1. Definition: Implements memory comparison.

  2. Note: Because the unit of comparison is bytes, pay attention to whether the machine is big-endian or little-endian.

  3. Function function: int memcmp( const void * buf1 , const void * buf2 , size_t count );  when buf1>buf2, return the number >0; when buf1<buf2, return the number <0; when buf1=buf2, return 0;

  4. function usage

3.1 memcmp use 

#include<stdio.h>
#include<string.h>

int main()
{
	int arr1[] = { 1,2,3,4,6 };
	int arr2[] = { 1,2,3,4,5 };
	printf("%d\n", memcmp(arr1, arr2, 20));
	return 0;
}
//结果是:1

4.memset

  1. Definition: Implements memory settings.

  2. Note: memset initializes the memory unit in bytes.

  3. Function function: void *memset( void * dest , int c , size_t count ); set the length of count bytes after the first address of the array pointed to by the dest pointer to integer c.

  4. function usage

4.1 memset use 

#include<stdio.h>
#include<string.h>

int main()
{
	int arr[] = { 0x11111111,0x22222222,0x33333333 };
	memset(arr, 0, 12);
	printf("ii");
	return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_59174190/article/details/124222989