The difference between memcopy and memmove

Source: Internet
Author: User
Tags count function prototype

The memcopy and Memmove functions look at the source code of two functions under Linux.

Two functions are defined in the header file string.h, and the function prototype is:

void * __cdecl memcpy (void * dst,const void * src,size_t count);
void * __cdecl memmove (void * dst,const void * SR c,size_t count);

The implementation code is as follows:

void * __cdecl memcpy (void * dst,const void * src,size_t count) {void * ret = DST;
                   while (count--) {//Note that the memcpy function does not handle the problem of overlapping DST and SRC regions * (char *) DST = * (char *) src;
                   DST = (char *) DST + 1;
         src = (char *) src + 1;
return (ret);
         } void * __cdecl memmove (void * dst,const void * src,size_t count) {void * ret = DST; if (DST <= src | | (char *) DST >= ((char *) SRC + count) {//If the DST and src regions do not overlap, copy them at the beginning WH
                            Ile (count--) {* (char *) DST = * (char *) src;
                            DST = (char *) DST + 1;
                   src = (char *) src + 1; } else {//if the DST and SRC regions Cross, copy from the tail to the starting position to avoid data collisions DST = (char *) DST + cou
                   nt-1;
                   src = (char *) src + count-1; While (count--)
                            {* (char *) DST = * (char *) src;
                            DST = (char *) dst-1;
                   src = (char *) src-1;
} return (ret); }

To sum up:

When the SRC and DST regions do not overlap, two functions are exactly the same. The wood has overlapping conditions: DST <= src | | (char *) DST >= ((char *) SRC + count. Otherwise, memcpy is not working properly, memmove can work normally.

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.