C++ strcpy function implementation

  • 2020-05-05 11:32:50
  • OfStack

Let's start with an example,


char * strcpy(char * strDest,const char * strSrc) {
  if ((NULL==strDest) || (NULL==strSrc)) 
    throw "Invalid argument(s)";
  char * strDestCopy = strDest;
  while ((*strDestCopy++ = *strSrc++) != '\0');
  return strDest;
}

I suddenly thought of an exam I did before,

Topic:
The known prototype of the strcpy function is
              char * strcpy(char * strDest,const char * strSrc);
      1. Implement strcpy function without calling library function.
      2. Explain why you want to return char *.

      explanation:
      1.strcpy implementation code


    char * strcpy(char * strDest,const char * strSrc)

    {

        if ((strDest==NULL)||(strSrc==NULL)) //[1]

            throw "Invalid argument(s)"; //[2]

        char * strDestCopy=strDest; //[3]

        while ((*strDest++=*strSrc++)!='/0'); //[4]

        return strDestCopy;

    }

      wrong:  

      [1]
      (A) does not check the validity of the pointer, indicating that the respondent does not care about the robustness of the code.
      (B) is used when checking the validity of the pointer ((! strDest) | | (! strSrc)) or (! (strDest&&strSrc)), indicating that the respondent has no deep understanding of the implicit conversion of types in C language. In this case, converting char * to bool is a type implicit conversion, which, while flexible, is more likely to result in higher error rates and higher maintenance costs. So C++ specifically added the three keywords bool, true, false to provide a more secure conditional expression.
      (C) is used to check the validity of the pointer ((strDest==0)||(strSrc==0)), indicating that the respondent is unaware of the benefits of using constants. Using literal constants directly, such as 0 in this example, reduces the maintainability of the program. Although 0 is simple, but there may be many points in the program to check the pointer, in case of a clerical error, the compiler can not find, the generated program contains logic errors, it is difficult to rule out. Using NULL instead of 0, the compiler checks for spelling errors.  

      [2]
      (A)return new string("Invalid argument(s)"); "Indicates that the respondent has no idea what the return value is used for and is not alert to memory leaks. It is very dangerous to return the memory allocated in the body of a function from a function. It throws the obligation to release memory onto an unsuspecting caller. Most of the time, the caller will not release memory, which leads to a memory leak.
      (B)return 0; , indicating that the respondents did not grasp the abnormal mechanism. The caller may forget to check the return value, and the caller may not be able to check the return value (see the chained expression below). The delusion that the return value bears the dual function of returning the correct value and the outlier often results in the failure of both functions. Instead of returning a value, throw an exception to lighten the burden on the caller, prevent errors from being ignored, and make the program more maintainable.  

    [3]
      (A)
      [4]
      (A) loop is written as while (*strDest++=*strSrc++); With the [1] (B).
      (B) loop is written as while (*strSrc! = '/ 0') * strDest strSrc + + + + = *; , which indicates that the examiners are not able to check the boundary conditions. At the end of the loop body, the strDest string did not end with '/0' correctly.

      2. Returning the original value of strDest enables the function to support chained expressions, increasing the "added value" of the function. Functions of the same function, if they can reasonably improve the availability, naturally more ideal.
      chain expression in the form of:
              int iLength=strlen(strcpy(strA,strB));
     
              char * strA=strcpy(new char[10],strB);
      returns the original value of strSrc incorrectly. First, the source string must be known, and there is no point in returning it. Second, you cannot support an expression that looks like the second example. Third, in order to protect the source string, const is used to define the content of strSrc, const char * as char * is returned, the type is inconsistent, compiler error.


Related articles: