Implementation of the strcpy function "Go"

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/chenyg32/p/3739564.html

The prototype of the known strcpy function is:

Char *strcpy (char *dst, const char *SRC);

    1. Implementing the strcpy function
    2. Explain why you want to return char *
    3. If you consider the case of DST and src memory overlap, strcpy how to implement

Implementation code for 1.STRCPY
char * strcpy (char *dst,const char *src)   //[1]{    assert (DST! = NULL && src! = null);    [2]    char *ret = DST;  [3]    while ((*dst++=*src++)! = ' + ');//[4]    return ret;}

[1] Const Retouching

The source string parameter is modified with a const to prevent the source string from being modified.

[2] NULL pointer check

(A) do not check the validity of pointers, stating that the respondents do not pay attention to the robustness of the code.

(B) Use ASSERT (!DST &&!src) when checking the validity of pointers;

char * conversion to BOOL is a type implicit conversion, although the functionality is flexible, but it is more likely to lead to increased error probability and higher maintenance costs.

(C) Use assert when checking the validity of pointers (DST! = 0 && src! = 0);

Using constants directly, such as 0 in this example, reduces the maintainability of the program. Instead of using null instead of 0, the compiler checks if a spelling error occurs.

[3] return to Destination address

(A) Forget to save the original STRDST value.

[4] ' + '

(A) The loop is written as while (*dst++=*src++); it is obviously wrong.

(B) The loop is written as while (*src!= ') *dst++=*src++;

After the loop body ends, the end of the DST string is not correctly appended with '/'.

2. Why should I return char *?

Returning the original value of DST enables a function to support chained expressions.

The form of a chain expression is as follows:

int L=strlen (strcpy (STRA,STRB));

Another example:

char * stra=strcpy (new CHAR[10],STRB);

It is wrong to return the original value of STRSRC.

One is that the source string is definitely known and it doesn't make sense to return it.

Second, it is not possible to support an expression that is shaped like the next.

The third, the const char * as char * return, the type does not match, compile error.

3. If you consider the case of DST and src memory overlap, strcpy how to implement

Char s[10]= "Hello";

strcpy (S, s+1); Should return to Ello,

strcpy (s+1, s); The Hhello should be returned, but the actual error will be due to the fact that DST overlaps with src and covers

The so-called overlap, that is, the SRC unhandled part has been overwritten by DST, there is only one situation: Src<=dst<=src+strlen (SRC)

c function memcpy memory overlap detection function, the following gives the implementation of memcpy my_memcpy.

char * strcpy (char *dst,const char *src) {    assert (DST! = NULL && src! = null);    char *ret = DST;    my_memcpy (DST, SRC, strlen (src) +1);    return ret;}

MY_MEMCPY is implemented as follows

Char *my_memcpy (char *dst, const char* src, int cnt) {    assert (DST! = NULL && src! = null);    char *ret = DST;     if (DST >= src && DST <= src+cnt-1)//memory overlap, start copying from high address    {        dst = dst+cnt-1;        src = src+cnt-1;        while (cnt--)            *dst--= *src--;    }    else    //normal, copying from low address    {        while (cnt--)            *dst++ = *src++;    }        return ret;}

Implementation of the strcpy function "Go"

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.