Usage, analysis and simulation implementation of C language memory functions

Table of contents

1. Memory copy function——memcpy

1. Function declaration:

Notice:

2. Function usage cases:

3. Simulation implementation of memcpy function:

2. Memory copy function 2—memmove

1. Function declaration:

2. Simulation implementation of memmove function

3. Memory comparison function——memcmp

1. Function declaration:

2. Function usage examples:

4. Memory setting function——memset

1. Function declaration:

2. Function usage examples:


1. Memory copy function——memcpy

1. Function declaration:

void * memcpy ( void * destination, const void * source, size_t num );

explain:

①: Function: Copy the first num bytes of the content of the space pointed by source to the space pointed by destination.

②: This function has three parameters:

The first parameter is a void* pointer, pointing to the destination space;

The second parameter is a void* pointer, used to point to the source space;

The third parameter type is size_t, which represents the number of bytes to be copied.

③: The return type of this function is void*, which is the starting address of the destination space.

④: This function is included in the header file <string.h>.

Notice:

1. When thinking of the word "copy", many posts may think of "strcpy", but it must be noted that the "strcpy" function can only be used to copy strings, while the "memcpy" function may copy different types of data (int *, char*,...etc.), so we can also see that the two pointers of the formal parameters of this function are both void* type, which is to receive different types of data.

2. The memcpy function can only be used to process non-overlapping memory copies (such as copying the contents of the array with subscripts 1,2,3,4,5 to the array with subscripts 3,4,5,6,7 position, in this case the memory spaces marked 3, 4, and 5 overlap), otherwise an error will occur. The copy of overlapping memory is implemented by another function-memmove, which is introduced below.

2. Function usage cases:

#include<stdio.h>
#include<string.h>
int main()
{
	//**********用例1***************************
	int arr1[10] = { 1,2,3,4,5,6,7,8,9,0 };
	int arr2[10] = { 0 };
	memcpy(arr2, arr1, sizeof(arr1));
	printf("arr2:");
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr2[i]);
	}
	printf("\n");
	//**********用例2****************************
	float arr3[5] = { 1.0,2.0,3.0,4.0,5.0 };
	float arr4[5] = { 0 };
	printf("arr4 ");
	memcpy(arr4, arr3, sizeof(arr3));
	for (int i = 0; i < 5; i++)
	{
		printf("%f ", arr4[i]);
	}
	return 0;
}

Use case running results:

3. Simulation implementation of memcpy function:

①: Source code

//函数模拟实现
#include<stdio.h>
#include<string.h>
#include<assert.h>

void* my_memcpy(void* dest, void* sour, size_t size)
{
	void* ret = dest;
	assert(dest && sour);
	while (size--)
	{
		*(char*)dest = *(char*)sour;
		dest = (char*)dest + 1;
		sour = (char*)sour + 1;
	}
	return ret;
}
int main()
{
	int arr1[5] = { 1,2,3,4,5 };
	int arr2[5] = { 0 };
	my_memcpy(arr2, arr1, sizeof(arr1));
	for (int i = 0; i < 5; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;
}

explain:

①: Because it is a simulation implementation, the return type and parameter type must be the same.

②: Because the return value is the starting address of the target space, a void* pointer is first created to save the starting address dest of the target space.

②: Because the void* pointer cannot be used for addition, subtraction and dereference operations, it must be forced to be converted to a pointer of other types, so it is necessary to think about what type it should be converted to? Because the unit is bytes, it is obvious that it can be copied byte by byte by converting it into a char* pointer. The so-called copying is nothing more than an assignment operation.

③: We know that copying is done in bytes, so the number of loops is the size of the number of bytes to be copied, so just use a while loop. Inside the loop body is the copy part. First convert the two pointers into char*. Pointer, so the dereferenced access permission is one byte, and the values ​​are assigned sequentially, and finally the starting address of the target space is returned ret.

2. Memory copy function 2—memmove

1. Function declaration:

void * memmove ( void * destination, const void * source, size_t num );

explain:

①: We found that the return type and parameter type of this function are the same as the "memcpy" function. It is not difficult to understand, because both are copies of memory, but the "memmove" function has a more advanced function, that is, it can Overlapping memory is copied.

2. Simulation implementation of memmove function

#include<stdio.h>
#include<string.h>
#include<assert.h>
void* my_memmove(void* dest, void* sour, size_t sz)
{
	assert(dest && sour);
	void* ret = dest;
	if (dest < sour)
	{
		//前->后
		while (sz--)
		{
			*(char*)dest = *(char*)sour;
			dest = (char*)dest + 1;
			sour = (char*)sour + 1;
		}
	}
	else
	{
		//后->前
		while (sz--)
		{
			*((char*)dest+sz) = *((char*)sour+sz);
		}
	}
	return ret;
}

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

operation result:

3. Memory comparison function——memcm p

1. Function declaration:

  

int memcmp ( const void * ptr1, const void * ptr2, size_t num );

explain:

①: The function of this function is to compare the content pointed to by ptr1 and the content pointed by ptr2, also in bytes, but it should be noted that the comparison is in bytes, and the third parameter num is The number of bytes to be compared.

②: Be careful not to be confused with the string comparison function "strcmp". "strcmp" can only be used to compare strings, while this function can compare different types of data.

②: Comparison rules: (refer to function "strcmp")

If the former is greater than the latter, return a number greater than 0;

If before is equal to after, return 0;

If the front is less than the back, return a number less than 0.

2. Function usage examples:

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

Obviously, the first four numbers of arr1 are the same as arr2, but the fifth number is smaller than the fifth number of arr2, so you will get a number less than 0. The vs compiler defaults to -1 for this number less than 0, which is greater than The number of 0 is 1.

4. Memory setting function——memset

1. Function declaration:

void * memset ( void * ptr, int value, size_t num );

①: The function of the function is to set the first num bytes of the content of the space pointed to by ptr to the value of the second parameter. Be sure to note that the unit is bytes. Please refer to the usage example.

②: The return value is the starting address of the space pointed to by ptr.

2. Function usage examples:

This is the end of this knowledge, I hope it is helpful to you!

Guess you like

Origin blog.csdn.net/hffh123/article/details/133209921