Implementation of strcpy Functions

Source: Internet
Author: User

This article from: norcy https://www.cnblogs.com/chenyg32/p/3739564.html (respect for labor results, welcome to read the original, invasion and deletion)

 

It is known that the prototype of the strcpy function is:

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

  1. Implement strcpy Functions
  2. Explain why char * is returned *
  3. If we consider how to implement strcpy when DST and SRC memory overlap
1. Implementation Code of strcpy
char * strcpy(char *dst,const char *src)   //[1]{    assert(dst != NULL && src != NULL);    //[2]    char *ret = dst;  //[3]    while ((*dst++=*src++)!=‘\0‘); //[4]    return ret;}

[1]Const Modification

The source string parameters are modified with const to prevent the source string from being modified.

[2]Null Pointer check

(A) if the pointer is not checked, it means that the respondent does not pay attention to the robustness of the Code.

(B) Use assert (! DST &&! SRC );

Char * is converted to bool by implicit type conversion. This function is flexible, but it increases the error probability and maintenance costs.

(C) Use assert (DST! = 0 & SRC! = 0 );

Using constants directly (such as 0 in this example) reduces the maintainability of the program. If null is used to replace 0, the compiler will check if a spelling error occurs.

[3]Return target address

(A) forget to save the original strdst value.

[4]'\ 0'

(A) The loop is written as while (* DST ++ = * SRC ++), which is obviously incorrect.

(B) cyclically write while (* SRC! = '\ 0') * DST ++ = * SRC ++;

After the loop body ends, '\ 0' is not correctly added to the end of the DST string '.

 

2. Why should char * be returned *?

Returns the original DST value so that the function supports chained expressions.

The form of a chained expression is as follows:

Int L = strlen (strcpy (stra, strb ));

Another example:

Char * stra = strcpy (New char [10], strb );

The original strsrc value is returned incorrectly.

First, the source string must be known and it does not make sense to return it.

Second, the expression in the second example cannot be supported.

Third, the const char * is returned as char *. The type is inconsistent and an error is returned during compilation.

 

3. If we consider how to implement strcpy when the DST and SRC memory overlaps

Char s [10] = "hello ";

Strcpy (S, S + 1); // ello,

// Strcpy (S + 1, S); // hhello should be returned, but an error will be reported because DST overlaps with SRC and overwrites '\ 0'

The so-called overlap means that the unprocessed parts of SRC have been overwritten by DST. There is only one situation: SRC <= DST <= SRC + strlen (SRC)

The C function memcpy comes with the memory overlap detection function. The following describes 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;}

The implementation of my_memcpy is 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 overlaps, copy {DST = DST + cnt-1; src = SRC + cnt-1; while (CNT --) * DST -- = * SRC --;} else // normal, copy {While (CNT --) * DST ++ = * SRC ++;} return ret;} from the lower address ;}

 

Implementation of strcpy Functions

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.