Memory copy processing function for the "C language" string

Source: Internet
Author: User

This blog post is mostly about writing memcpy functions in the library,

For strings, we use the function of the str category in string <string.h>, but we also have some copy functions about memory. The object they manipulate is memory and can then accept any type of data for copying.

This is the memcpy in <memory.h>, and then we'll take a look at MSDN and take a look at his prototype:

void *memcpy (void *dest,const void *src,size_t count);

Unlike strcpy, the third parameter is added, the number of bytes of the operation is determined, and then the parameter type and return type are void*

, which means that he can copy any type of data.

Then we look at the implementation:

memcpy


void *my_memcpy (Void *str,const void *dstr,int count)                            //from the memory address to change, and determine the length of change, so with a universal type to accept {              char *pstr =  (char *) str;              char *pDstr =  (char *) dstr;             assert ((str!=null)  &&  (dstr != null));              if (STR&NBSP;==&NBSP;DSTR)                                             //in the same positionIn case of a direct return to the change                           return  (char *) dstr;              while (count-- > 0)              {                         *pstr++ = *pdstr ++;            }              return str;}

Then there is a problem, if we copy the data in the DSTR start position between the Str operation, then the change Str will have side effects, will cause our copy result is not correct, so we should consider the situation of coverage. There is a memmove function in the function library.

Memmove

Void *my_memmove (void *pst,const void *dpst,int size) {                         void *p = pst;              char *psta =  (char *) pst;              char *pstB =  (char *) dpst;             assert ((pst != null)  && (dpst != null));              if (Pstb<psta< pstb+size)             {                               &nbSp;       while (size--)                          {                                      * (PstA+size)  = * (pstb+size);                         }             }             else             {                           while (size--)                          {                                      *pstA++ = *pstB++;                         }             }              return p;}

The copy memory overlay is encountered when the copied space starts in the copy space. In this case we will consider copying from the tail. So the judgment was made.

This article is from the "egg-left" blog, please be sure to keep this source http://memory73.blog.51cto.com/10530560/1697227

Memory copy processing function for the "C language" string

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.