C language-memory copy handler for strings

Source: Internet
Author: User

C language-memory copy handler for strings
For strings, we use the str class function in the string <string. h>, but we also have some copy functions about memory. They operate on the memory and can then copy any type of data. This is <memory. h> In memcpy, let's take a look at MSDN and see its prototype: void * memcpy (void * dest, const void * src, size_t count ); different from strcpy, a third parameter is added to determine the number of bytes for the operation, and the parameter type and return type are all void *, which means that it can copy any type of data. Then let's take a look at the implementation: memcpy:

Void * my_memcpy (void * str, const void * Dstr, int count) // change the memory address and determine the length, therefore, the {char * pstr = (char *) str; char * pDstr = (char *) Dstr; assert (str! = NULL) & (Dstr! = NULL); if (str = Dstr) // return the return (char *) Dstr to be changed if the location is the same; while (count --> 0) {* pstr ++ = * pDstr ++;} return str ;}

 

Then there will be a problem. If the starting position of the Dstr In the copied data is between the STR operations, there will be side effects when str is changed, which will lead to incorrect copying results, therefore, we should consider the 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)            {                                     while(size--)                        {                                    *(pstA+size) = *(pstB+size);                        }            }             else            {                         while(size--)                        {                                    *pstA++ = *pstB++;                        }            }             return p;}

 

It means that the copied space starts in the copy space and overwrites the copy memory. In this case, we will consider copying from the end. So I made a judgment.

Related Article

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.