In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the C++ pen test questions with high frequency". The content in the article is simple and clear, and it is easy to learn and understand. Now please follow the editor's train of thought slowly and deeply. Let's study and learn "what are the C++ pen test questions with high frequency"?
Two highly frequent C++ written test questions:
The known prototype of the strcpy function is:
Char * strcpy (char * strDest,const char * strSrc)
1. Implement the strcpy function without calling the library function.
two。 Explain why char * is returned.
Commentary:
The implementation code of 1.strcpy
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;}
The wrong approach:
[1]
(a) do not check the validity of the pointer, indicating that the respondent does not pay attention to the robustness of the code.
(B) use (! strDest) | | (! strSrc)) or (! (strDest&&strSrc)) when checking the validity of the pointer, indicating that the respondent does not have a deep understanding of the implicit conversion of types in C language. In this case, the conversion from char * to bool is an implicit type conversion, which is flexible but more likely to increase the probability of error and the cost of maintenance. So C++ specially added three keywords bool, true and false to provide more secure conditional expressions.
(C) use ((strDest==0) | | (strSrc==0)) when checking the validity of pointers, indicating that the respondent is not aware of the benefits of using constants. The direct use of literal constants, such as 0 in this case, reduces the maintainability of the program. Although 0 is simple, there may be many checks for pointers in the program. In case of a pen error, the compiler cannot find that the generated program contains logic errors, which is difficult to eliminate. Using NULL instead of 0, if there is a spelling error, the compiler will check it out.
[2]
(a) return new string ("Invalid argument (s)"); which means that the respondent has no idea what the return value is for, and he is not wary of memory leaks. It is very dangerous to return the memory allocated in the function body from the function. He throws the obligation of releasing memory to the unwitting caller, who in most cases does not release memory, which leads to memory leak.
(B) return 0 position, indicating that the respondent 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 chained expression below). The delusion of making the return value shoulder the dual function of returning correct values and outliers often results in the failure of both functions. The return value should be replaced by throwing an exception, which can lighten the burden on the caller, prevent errors from being ignored, and enhance the maintainability of the program.
[3]
(a) forgot to save the original strDest value, indicating that the respondent's logical thinking is not strict.
[4]
(a) write the loop as while (* strDest++=*strSrc++);, as well as [1] (B).
(B) the loop is written as while (* strSrcised conditions'\ 0') * strDest++=*strSrc++;, to show that the interviewees did not check the boundary conditions properly. At the end of the loop body, the'\ 0' is not correctly added to the end of the strDest string.
two。 Returning the original value of strDest enables the function to support chained expressions, increasing the "added value" of the function. The function of the same function, if it can reasonably improve the availability, is naturally more ideal.
The form of a chain expression is as follows:
Int iLength=strlen (strcpy (strA,strB))
Another example is:
Char * strA=strcpy (new char [10], strB)
It is wrong to return the original value of strSrc. For one thing, the source string must be known, and there is no point in returning it. Second, the expression like the second example cannot be supported. Third, in order to protect the source string, the formal parameter uses const to define what strSrc refers to, and returns const char * as char *. The type does not match and the compilation error is reported.
Thank you for your reading. The above is the content of "what are the C++ pen test questions with ultra-high frequency?" after the study of this article, I believe you have a deeper understanding of what the C++ pen test questions with high frequency have. The specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.