Implementation of several library functions for Memmove and memcpy and strcmp strcpy

Source: Internet
Author: User

Memmove and memcpy

1.memmove

Function prototypes: void *memmove (void *dest, const void *source, size_t count)

Return value Description: Returns the void * pointer to dest

Parameter description: Dest,source is the destination string and the first address of the source string, respectively. Count is the number of characters to move

Function Description: Memmove is used to copy count characters from source to dest, and if the target region and source area overlap, memmove can ensure that the source string copies the bytes of the overlapping region to the target area before being overwritten.

2.memcpy

Function prototypes: void *memcpy (void *dest, const void *source, size_t count);

Return value Description: Returns the void * pointer to dest

Function Description: The memcpy function and Memmove are the same, but the regions in dest and source in memcpy cannot overlap, otherwise unknown results occur.

3. Difference between the two

The function memcpy () copies count characters from the area to which the source points to dest, and does not define the behavior of the function if the two arrays overlap.
and Memmove (), if the two functions overlap, the assignment is still correct.

The memcpy function assumes that there is no overlap in the area of memory to be copied, and if you can make sure that the memory area of your copy operation does not overlap, you can use memcpy directly;
If you cannot guarantee that there is overlap, in order to ensure the correctness of the copy, you must use Memmove.

memcpy will be more efficient than memmove, and if you don't understand it, you can see some of the two implementations:

Memmove

void*memmove (void*dest,void Const*src, size_t N) {RegisterChar*DP =dest; RegisterChar*SP =dest; if(DP <sp) {                    while(n-->0)                      *dp++ = *sp++; }          Else          {                   //we should do the copy reverselyDP + =N; SP+=N;  while(n-->0)                          *--DP = *--sp; }  }  

memcpy

void *memcpy (voidconstvoid *src, size_t count) {   char *tmp = dest;    Const char *s = src;     while (count--)          *tmp++ = *s++;    return dest;}

STRCMP implementation

intstrcmp (Const Char* SRC,Const Char*DST) {        intRET =0 ;  while( ! (ret = * (unsignedChar*) src-* (unsignedChar*) DST) && *DST)++SRC, + +DST; if(Ret <0) ret= -1 ; Else if(Ret >0) ret=1 ; return(ret);}

strcpy implementation

Char* strcpy (charconstChar*char* tmp =while  '+')  return  dest;}

Implementation of several library functions for Memmove and memcpy and strcmp strcpy

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.