[Conversion] Implementation of strcpy Function

Source: Internet
Author: User

Conversion from: Implementation of strcpy Function

Code Implementation

#include <iostream>#include <assert.h>#include <iostream>//#include <string.h>using namespace std;int strlen(const char *src){    assert(src != NULL);    int lens = 0;    while (*src++ != ‘\0‘)        lens++;    return lens;}char *memcpy(char *dst, const char *src, int cnt){    assert(dst != NULL && src != NULL);    char *ret = dst;    if (dst >= src && dst <= src + cnt - 1)    {        src += cnt - 1;        dst += cnt - 1;        while (cnt--)            *dst-- = *src--;    }    else    {        while (cnt--)            *dst++ = *src++;    }    return ret;}char *strcpy(char *dst, const char *src){    assert(dst != NULL);    assert(src != NULL);    char *ret = dst;    memcpy(dst, src, strlen(src)+1);    return ret;}int main(){    char a[] = "hello";    char b[] = "b1";    //strcpy(a, b);    strcpy(a, a+1);    char *f = strcpy(a, a+1);    cout << a << endl;    cout << f << endl;    int m = 0;    int n = 5;    int c = (m=n++);    cout << "c:" << c << endl;    cout << "n:" << n << endl;    return 0;}
View code

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

  • If the pointer validity is not checked, it means that the respondent does not pay attention to the robustness of the Code.
  • Use assert (! DST &&! SRC); char * To bool is implicit type conversion. Although this function is flexible, it is more likely to increase the error probability and increase the maintenance cost.
  • Use assert (DST! = 0 & SRC! = 0); directly using constants (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

  • Forget to save the original strdstt value.

[4]'\ 0'

  • The loop is written as while (* DST ++ = * SRC ++), which is obviously incorrect.
  • Write the 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 ;}

[Conversion] Implementation of strcpy Function

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.