Overriding the strcpy () function prototype

Source: Internet
Author: User

The prototype of the known strcpy function is
char* strcpy (char* strdest,const char* strsrc);
1. Do not call library function, implement strcpy function
2. Explain why you should return to char*;

Implementation code for 1.STRCPY

char* strcpy (char* strdest,const char* strsrc)
{
 if (strdest = NULL) | | (strsrc = = NULL)) [1]
  throw "Invalid Arguments";//[2]
 char* strdestcopy = strdest;//[3] While
 ((*strdest++ = *strsrc++)! = ' /0 ')//[4]
  ;
 return strdestcopy;
}

[1]
(A) not checking the validity of pointers, indicating that the designer does not pay attention to the robustness of the program
(B) Use when checking the validity of pointers ((!strdest) | | (!STRSRC)) OR (!) ( strdest && strsrc)), stating that the C language
There is no deep understanding of the type implicit conversion, in this case the char* conversion to bool is the type implicit conversion, although the function is flexible, but
is more likely to lead to increased error probability and higher maintenance costs
(C) Use when checking the validity of pointers ((strdest==0) | | (strsrc==0)) Indicates that the user does not know the benefits of using constants. Using literal constants directly, such as 0 in this example, reduces the maintainability of the program. 0 Although simple, but there may be many places in the program to check the pointer, in case of a clerical error, the compiler can not find that the generated program contains logic errors, it is difficult to exclude. Instead of using null instead of 0, the compiler checks if a spelling error occurs.
[2]
(A) return new string ("Invalid arguments"), stating that the answer does not know that the return is worthwhile, and that it is not alert to memory leaks, it is dangerous to return memory allocated from the function body. He throws the obligation to release memory to unsuspecting callers, in the vast majority of cases, the caller does not release memory, which leads to memory leaks
(B) return 0, stating that the answer does not have an abnormal mechanism. It is possible for the caller to forget to check the return value, and the caller may not be able to check the return value (see following chain expression). Paranoia allows the return value to assume the dual function of returning the correct value and outliers, often with the result that both functions fail. The return value should be replaced by throwing an exception, which can relieve the burden on the caller, make the error not be ignored, and enhance the maintainability of the program.
[3]
(A) Forget to save the original strdest value, indicating that the answer is not rigorous logic.
[4]
(A) The loop is written as while (*strdest++=*strsrc++), with [1] (B).
(B) The loop is written as while (*strsrc!= '/0 ') *strdest++=*strsrc++, which indicates that the examination of the boundary condition by the answer is poor. After the loop body ends, the end of the strdest string is not correctly appended with '/0 '.
2. Returning the original value of Strdest enables the function to support chained expressions, adding "added value" to the function. Functions of the same function, if it can reasonably improve the availability of natural more ideal. The form of a chain expression is as follows:
int Ilength=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. Third, in order to protect the source string, the formal parameter with the const limit STRSRC content, the const char * as char * return, the type does not match, compile error.

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.