An in-depth analysis of common face questions for C + + programmers (1)

Source: Internet
Author: User
Tags assert

Excerpt from: http://blog.csdn.net/zhoudengqing 1. IntroductionThe purpose of this paper is not to provide C/T Programmers job interview guidance, but to technically analyze the connotation of the interview question. Most of the questions in the paper come from the major forums, some of the answers to the questions also refer to the views of netizens. Many face questions seem simple, but need deep basic skills to give the perfect answer. Enterprises require the interviewer to write a simplest strcpy function can see how the interviewer in the technical level, we can really write a strcpy function? We all think we can, but the strcpy we write will probably only get 2 points in 10 points. Readers can see from this article the strcpy function from 2 to 10 of the examples of answers, to see what level they belong to. In addition, there are some questions to examine the interviewer's ability to think quickly. The analysis of these questions has a strong interest in itself, and as a research and development staff, through the deep analysis of these questions can further enhance their internal strength. 2. Find the wrong question Question 1:void Test1 () {char string[10];     char* str1 = "0123456789"; strcpy (string, str1);} Question 2:void Test2 () {char string[10], str1[10];     int i;     for (i=0; i<10; i++) {Str1[i] = ' a '; } strcpy (String, str1);} Question 3:void Test3 (char* str1) {char string[10];     if (strlen (str1) <=) {strcpy (string, str1); }} Answer:Question 1 The string str1 requires 11 bytes to be stored (including the end of ' + '), while the string has only 10 bytes of space, strcpy will cause the array to be out of bounds; for question 2, if the interviewer points out that the character array str1 cannot end within the array can give3 minIf the interviewer points out that the strcpy (string, str1) call makes it possible to copy the number of bytes copied from str1 memory to string memory with uncertainty7 min, this paper points out that the library function strcpy the way to work.10 minThe question 3,if (strlen (str1) <= 10) should be changed to if (Strlen (STR1) < 10) Because the result of Strlen does not count the 1 bytes occupied by '% '. Anatomy:To examine the mastery of the Basic skills: (1) The string ends with ' n ', (2) The sensitivity of the logarithm of the cross-border, and (3) the way the library function strcpy, if you write a standard strcpy function with a total score of 10, here are answers to several different scores: 2 minvoid strcpy (char *strdest, char *strsrc) {while ((*strdest++ = * strsrc++)! = ' + ');} 4 minvoid strcpy (char *strdest, const char *STRSRC)//Adds the source string to a const, indicating that it is an input parameter, plus 2 minutes {while ((*strdest++ = * strsrc++)! = ' + '); } 7 minvoid strcpy (char *strdest, const char *STRSRC) {//For source address and destination address plus non 0 assertion, plus 3 point assert ((strdest! = null) && (STRSRC! = null) ); while ((*strdest++ = * strsrc++)! = ') ';} 10 minIn order to realize the chain operation, return the destination address, add 3 points!  char * strcpy (char *strdest, const char *strsrc) {assert ((strdest! = null) && (STRSRC! = null));   char *address = strdest;  while ((*strdest++ = * strsrc++)! = ' \ "); return address;} From 2 points to 10 points of several answers we can clearly see, the small strcpy unexpectedly hidden so many mystery, really not cover! Need how solid basic skills to write a perfect strcpy Ah! (4) The mastery of strlen, which does not include the end of the string ' \ '.    Readers look at the different scores of the strcpy version, you should also be able to write a 10-point strlen function, the perfect version is: int strlen (const char *STR)//input parameter const{assert (strt! = NULL);     Assertion string address is not 0 int len;     while ((*str++)! = ' + ') {len++; } return len; Question 4:void GetMemory (char *p) {p = (char *) malloc (100);} void Test (void) {char *str = NULL; GetMemory (str); strcpy (str, "Hello World");p rintf (str);} Question 5:Char *getmemory (void) {char p[] = "Hello World"; return p;      }void Test (void) {char *str = NULL;          str = GetMemory ();   printf (str); } Question 6:void GetMemory (char **p, int num) {*p = (char *) malloc (num);}     void Test (void) {char *str = NULL;     GetMemory (&AMP;STR, 100);     strcpy (str, "Hello"); printf (str);} Question 7:void Test (void) {char *str = (char *) malloc (100);     strcpy (str, "Hello");     Free (str); ...//omitted other statements} Answer:Question 4 The formal parameter passed into the GetMemory (char *p) function is a string pointer, modifying the parameter inside the function does not really change the value of the incoming parameter, and executes the char *str = NULL;           GetMemory (str), after which STR is still null, question 5, char p[] = "Hello World"; return p; P[] Array is a local automatic variable within a function, and after the function returns, the memory is freed. This is a common mistake many programmers make, the root of which is the failure to understand the lifetime of a variable. The getmemory of question 6 avoids the problem of question 4, the parameter passed in GetMemory is a pointer to a string pointer, but executes the request memory and assignment Statement *p = (char *) malloc (num) in GetMemory, and does not determine if the memory is successfully applied. Should be added: if (*p = = NULL) {...//to request memory failure processing} Question 7 has the same problem with question 6, in the execution of char *str = (char *) malloc (100), after the memory has not been applied for a successful judgment; ) After leaving Str blank, it is possible to become a "wild" pointer, plus: str = NULL, and the test function of question 6 does not release malloc's memory. Anatomy:The Test 4~7 examines the interviewer's understanding of the memory operation, and the basic skills of the interviewer are generally able to correctly answer the 50~60 errors. But it's not easy to answer it all right. The memory operation is mainly focused on: (1) Understanding of pointers, (2) The lifetime and scope of variables, and (3) Good dynamic memory application and release habits.     See what's wrong with the following program: Swap (int* p1,int* p2) {int *p;     *p = *P1;     *P1 = *P2; *P2 = *p;} In the Swap function, p is a "wild" pointer, which may point to the system area, causing the program to run in a crash. The debug runtime in VC + + prompts the error "Access violation".     The procedure should read: Swap (int* p1,int* p2) {int p;     p = *p1;     *P1 = *P2; *P2 = P;}

An in-depth analysis of common face questions for C + + programmers (1)

Related Article

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.