Beauty of Programming - Memory Functions

Keywords: Python Java C C++

Memory function

memcpy memory copy

  • The function memcpy copies num bytes of data backwards from the source location to the destination's memory location.
  • This function does not stop when it encounters'\0'.
  • If there is any overlap between source and destination, the results of the replication are undefined.

Original Format

Analysis

** Literally means that anything in memory can be copied, so the spell of string copy is broken and any type can be copied. It is not necessary to think about it. It must be related to the universal type (universal type pointer-untyped pointer) void* because qsort was still impressive at that time.

/*num Is the meaning of a few bytes*/
void* my_memcpy(void* dest, const void* src, size_t num)
{
	assert(dest && src);
	void* ret = dest;
	while (num--)
	{
		//Use char* with void* to split into the smallest and pass one by one
		*(char*)dest = *(char*)src;
		((char*)dest)++;//void*no type bad add directly, strong char*add
		((char*)src)++;
	}	
	return ret;
}

Memory copy problem

1. If memory is connected, it will copy errors

Then how do you solve the memory-related problems without the above errors? The memory space that is intersected by positive assignments will be operated twice, and the original value will be changed. What can we do? If you come from the back, the first two operations will change the later one, then take the latter assignments away first, then indirectly change the original situation, so the above generationThe code has to be modified. This is a case of copying backwards. If the entry is copied backwards, is there a problem from the back, but it will be perfect from the front, so we have to take both into account

So to solve the problem of overlapping copies, there is a function called memmove.

2. There is not enough memory to copy the direct program inside and hang up

memmove memory overlap copy

Used to handle memory overlaps

C Language Regulations

memcpy can handle copies of memory that do not overlap

memmove handles overlapping memory copies

We rewrite the memcpy code to meet the C language requirements, memcpy actually overfulfilled the task in the vs compiler, and his effect is already the same as that of memmove

You'll find that they run the same way, so I tested the test above with my own code (I've reached the C standard)

What's more subtle about memmove?

Original Format

Analysis

/*num Is the meaning of a few bytes*/
void* my_memmove(void* dest,const void* src, size_t num)//The parameters for memmove and memcpy are the same
{
	assert(dest && src);
	void* ret = dest;
	if (dest < src)
	{
		while (num--)
		{
			//sre memory copies from front to back
			//Use char* with void* to split into the smallest and pass one by one
			*(char*)dest = *(char*)src;
			((char*)dest)++;//void*no type bad add directly, strong char*add
			((char*)src)++;
		}
	}
	else
	{
		while (num--)
		{
			//sre memory copy from back to front
			//Use char* with void* to split into the smallest and pass one by one
			*((char*)dest+num) = *((char*)src+num);
			//((char*)dest)++;//void*no type bad add directly, strong char*add
			//((char*)src)++;
		}
	}	
	return ret;
}

memset memory settings

Sets the buffer to the specified character.

Original Format

Analysis

//The variable c represents the ASCII code of the character
void* my_memset(void* dest, int c, size_t count)
{	
	const unsigned char uc = c;    //Convert c to a character and not const	
	void* ret = dest;
	 int i = 0;
	 for (i = 0; i < count; i++)
	 {
		*(char*)dest = uc;
		((char*)dest)++;
	 }
	 return ret;
}

memcmp memory comparison

Similar to strcmp, but just a comparison string, a comparison memory, since you don't know what type, there is a byte limit behind it, which should exactly be similar to strncmp, because there is a parameter with a number behind it

Original Format

Analysis

Basically like string comparison, it's just a memory comparison

//The contents in buf1 memory are greater than those in buf2 memory > 0, and vice versa <0
int my_memcmp(const void* buf1, const void* buf2, size_t count)
{
	assert(buf1 && buf2);
	while (--count && *(char*)buf1 == *(char*)buf2)//This minus first is the detail
	{
		((char*)buf1)++;
		((char*)buf2)++;
	}
	if (*(char*)buf1 - *(char*)buf2 > 0)
		return 1;
	if (*(char*)buf1 - *(char*)buf2 < 0)
		return -1;
	return 0;
}

Don't overrun count for you, because it operates on memory and does not have the function of string completion\0

Posted by bo0 on Sun, 12 Sep 2021 10:54:52 -0700