In-depth analysis of common interview questions for C/C ++ programmers

Source: Internet
Author: User

InC/C ++In the interview process of programmers, many interview questions seem simple, but they need profound basic skills to give a perfect answer. Enterprises require the interviewer to write the simplest strcpy function to see how technically the interviewer has achieved. Can we really write a strcpy function? We all think we can, but the strcpy we write may only get 2 out of 10 points. In this article, you can see examples of strcpy functions from 2 to 10 to see what level they belong.

Locate the wrong question

Question 1:

 
 
  1. The following is a reference clip:
  2. Void test1 ()
  3. {
  4. Char string [10];
  5. Char * str1 = "0123456789 ";
  6. Strcpy (string, str1 );
  7. }

Question 2:

 
 
  1. The following is a reference clip:
  2. Void test2 ()
  3. {
  4. Char string [10], str1 [10];
  5. Int I;
  6. For (I = 0; I <10; I ++)
  7. {
  8. Str1 = 'a ';
  9. }
  10. Strcpy (string, str1 );
  11. }

Question 3:

 
 
  1. The following is a reference clip:
  2. Void test3 (char * str1)
  3. {
  4. Char string [10];
  5. If (strlen (str1) <= 10)
  6. {
  7. Strcpy (string, str1 );
  8. }
  9. }

Answer:

Question 1 string str1 requires 11 bytes to be stored (including '\ 0' at the end), while string only has 10 bytes of space, strcpy will cause the array to cross-border;

For question 2, if the subject points out that the character array str1 cannot end in the array, it can give 3 points; if the subject points out strcpy (string, str1) calling makes the number of bytes replicated from the str1 memory to the string memory uncertain. It can be given 7 points. Based on this, it is pointed out that the strcpy function is working for 10 points;

For question 3, if (strlen (str1) <= 10) should be changed to if (strlen (str1) <10 ), the result of strlen does not count the 1 byte occupied by '\ 0.

Analysis:

Measure the test taker's knowledge about basic skills:

(1) The string ends with '\ 0;

(2) sensitivity to array out-of-bounds control;

(3) how the database function strcpy works. If the total score of a standard strcpy function is 10, the following are several different answers:

2 points

 
 
  1. The following is a reference clip:
  2. Void strcpy (char * strDest, char * strSrc)
  3. {
  4. While (* strDest ++ = * strSrc ++ )! = '\ 0 ');
  5. }

4 points

 
 
  1. The following is a reference clip:
  2. Void strcpy (char * strDest, const char * strSrc)
  3. // Add the source string to const, indicating that it is an input parameter and adds 2 points
  4. {
  5. While (* strDest ++ = * strSrc ++ )! = '\ 0 ');
  6. }

7 points

 
 
  1. The following is a reference clip:
  2. Void strcpy (char * strDest, const char * strSrc)
  3. {
  4. // Add non-0 assertions to the source and target addresses, and add 3 points
  5. Assert (strDest! = NULL) & (strSrc! = NULL ));
  6. While (* strDest ++ = * strSrc ++ )! = '\ 0 ');
  7. }

10 points

 
 
  1. The following is a reference clip:
  2. // For chained operation, add 3 points to return the destination address!
  3. Char * strcpy (char * strDest, const char * strSrc)
  4. {
  5. Assert (strDest! = NULL) & (strSrc! = NULL ));
  6. Char * address = strDest;
  7. While (* strDest ++ = * strSrc ++ )! = '\ 0 ');
  8. Return address;
  9. }

We can clearly see from two to ten answers that the little strcpy has hidden so many xuanjicang! What a solid basic skill is required to write a perfect strcpy!

(4) Master strlen, which does not include '\ 0' at the end of the string '.

After reading strcpy versions with different scores, you can write a strlen function with 10 points. The perfect version is int strlen (const char * str). // enter the const parameter.

Question 4:

 
 
  1. The following is a reference clip:
  2. Void GetMemory (char * p)
  3. {
  4. P = (char *) malloc (100 );
  5. }
  6. Void Test (void)
  7. {
  8. Char * str = NULL;
  9. GetMemory (str );
  10. Strcpy (str, "hello world ");
  11. Printf (str );
  12. }

Question 5:

 
 
  1. The following is a reference clip:
  2. Char * GetMemory (void)
  3. {
  4. Char p [] = "hello world ";
  5. Return p;
  6. }
  7. Void Test (void)
  8. {
  9. Char * str = NULL;
  10. Str = GetMemory ();
  11. Printf (str );
  12. }

Question 6:

 
 
  1. The following is a reference clip:
  2. Void GetMemory (char ** p, int num)
  3. {
  4. * P = (char *) malloc (num );
  5. }
  6. Void Test (void)
  7. {
  8. Char * str = NULL;
  9. GetMemory (& str, 100 );
  10. Strcpy (str, "hello ");
  11. Printf (str );
  12. }

Question 7:

 
 
  1. The following is a reference clip:
  2. Void Test (void)
  3. {
  4. Char * str = (char *) malloc (100 );
  5. Strcpy (str, "hello ");
  6. Free (str );
  7. ... // Other omitted statements
  8. }

Answer:

In question 4, the form parameter of the GetMemory (char * p) function is a string pointer. Modifying the form parameter within the function does not actually change the value of the input form parameter.

Char * str = NULL;

GetMemory (str );

The subsequent str is still NULL;

Question 5

Char p [] = "hello world ";

Return p;

The p [] array is the partial automatic variable in the function. After the function returns, the memory has been released. This is a common mistake made by many programmers. Its root cause is that they do not understand the survival of variables.

GetMemory in question 6 avoids Question 4. The input parameter of GetMemory is a pointer to the string pointer, but the request memory and the value assignment statement tiffanybracelets is executed in GetMemory.

* P = (char *) malloc (num );

If the memory application is successful, add:

If (* p = NULL)

{

... // Handle the Memory Request failure

}

Question 7 has the same problem as question 6.

Char * str = (char *) malloc (100 );

In addition, after free (str), str is not set to null, which may become a "wild" pointer. The following should be added:

Str = NULL;

In the Test function of question 6, the memory of malloc is not released.

Analysis:

Question 4 ~ 7. Measure the test taker's understanding about memory operations. Generally, 50-50 of the respondents with solid basic skills can answer these questions correctly ~ 60 error. However, it is not easy to answer the correct questions completely.

The memory operations are mainly focused on:

(1) pointer understanding;

(2) The survival time and scope of the variable;

(3) good dynamic memory application and release habits.

Let's take a look at the following program errors:

 
 
  1. The following is a reference clip:
  2. Swap (int * p1, int * p2)
  3. {
  4. Int * p;
  5. * P = * p1;
  6. * P1 = * p2;
  7. * P2 = * p;
  8. }

In the swap function, p is a "wild" pointer, which may point to the system zone, causing program running to crash. In VC ++, the "Access Violation" error is prompted during DEBUG ". The program should be changed:

 
 
  1. The following is a reference clip:
  2. Swap (int * p1, int * p2)
  3. {
  4. Int p;
  5. P = * p1;
  6. * P1 = * p2;
  7. * P2 = p;
  8. }

In short, the interview questions of C/C ++ programmers not only examine the knowledge level of the subject, but also examine the agile Thinking Ability of the subject. As a R & D Engineer, through in-depth analysis of these interview questions, you can further enhance your internal strength.

Visual C ++ development practices 1200 (Volume I)

C ++ exploring: 68 lectures on C ++

C # debugging from entry-level to proficient

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.