Memory Operation function Memmove,memcpy,memset

Source: Internet
Author: User
Tags assert function prototype

By learning the string, we know that the Operation object of the string manipulation function is a string, and that its end sign is Terminator, and of course this says no

The restricted string function. However, when we want to copy a piece of memory data to another chunk of memory, we cannot use string manipulation functions, and of course

cannot be replicated in one, so a memory operation function is introduced.

memcpy function Prototype:

void *memcpy (void *dst, const void *SRC, size_t size); Copy data from one piece of memory to another piece of memory. When two pieces of memory overlap, memcpy can not guarantee that the copy is correct.

Memmove function Prototype:

void *memmove (void *dst, const void *SRC, size_t size); You can copy memory overlapping or copy without overlapping.

The following simulates the implementation of the memcpy function:

#define _crt_secure_no_warnings 1
#include <stdio.h>
#include <assert.h>
void *my_memcpy ( void *dest, const void *SRC, size_t size)
{
	assert (dest);
	ASSERT (SRC);
	Char *str1 = (char *) dest;
	const char *STR2 = (const char *) src;
	while (size)
	{
		*str1 = *str2;
		str1++;
		str2++;
		size--;
	}
	return dest;
}
int main ()
{
	char arr[] = "Hel love for You";
	void *ret = my_memcpy (arr, "YLRR", 3);
	Char *p = (char *) ret;
	printf ("%s", p);
	System ("pause");
	return 0;
}
Code Analysis: The function has three parameters, two is a void pointer, and the last is a shape variable that represents the number of bytes in the copy memory.

Why write a void pointer? The main purpose is to be able to receive various types of pointers, because we sometimes want to copy the area is to store the number of plastic, and sometimes want to copy the area is stored strings, we can not always according to the type of memory area data to change the function parameters, so much trouble ah ~ ~ So with void* is better ~~

The return value of the function is the void* type, which is similar to the argument above.

In addition, it should be noted that the void type pointer can not be self-added operation, and can not be used to dereference, so the function has been forced type conversion. The force is converted to char * because the operation is a byte in a byte. Like what:

void *p;

p++;//Error

*p = 1;//Error

Below we simulate the implementation of the Memmove function:

First of all, analyze the memmove implementation of the copy of several cases: the following figure

As we can see from the diagram, when Dest>src && dest < src+size, we need to copy backwards from the back, and other cases are copied back in the past.

The code is as follows:

#define _crt_secure_no_warnings 1
#include <stdio.h>
#include <assert.h>
<pre name= "code "Class=" OBJC ">void* my_memmove (void *dest, const void *SRC, size_t size)
{
	assert (dest);
	ASSERT (SRC);
	Char *str1 = (char *) dest;
	const char *STR2 = (const char *) src;
	if (str2 > Str1) && (str2< str1 + size) {while
		(size)
		{
			*str1 = *str2;
			str1++;
			str2++;
			size--
		}
	}
	else
	{while
		(size--)
		{
			* (str1 + size) = * (str2 + size);
		}
	}
	return dest;
}
int main ()
{
	char arr[] = "abcdef";
	void *ret = My_memmove (arr+2,arr,4);
	printf ("%s", arr);
	System ("pause");
	return 0;
}


Code Analysis: The code given in the test case is from the forward copy, the program runs the result is ABABCD, in the program output, can not output the return value, because the general situation that is wrong, the specific reason is actually very simple.

The previous backward copy is simpler, directly assigned, and then added; from the forward copy, when the first copy, the target string and the original string are backward offset size-1, in the program code, when the while loop condition judgment, the size of the self minus, so into the loop body, Size has been reduced by 1. So what you see is the offset size.

memset function:

Function prototype: void* memset (void *dest,int c,size_t size); gives the starting address of the memory, the size byte that will start from the starting address

The assignment is character C. This function is relatively simple to implement.

void * My_memset (void *dest, int c, size_t size)
{
	assert (dest);
	Char *pdest = (char *) dest;
	while (size--)
	{
		*pdest++ = c;
	}
	return dest;
}
int main ()
{
	char arr[] = "abcdef";
	My_memset (arr, ' 0 ', 3);
	printf ("%s", arr);
	System ("pause");
	return 0;
}
Note: The character given in the function is shaping, and this assignment does not force type conversion because the character variable is stored in memory in its ASCII form ~ ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.