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

Source: Internet
Author: User
C ++ programmers apply for common interview questions in-depth analysis 1 -- Linux general technology-Linux technology and application information, the following is a detailed reading. 1. Introduction

The purpose of this article is not to provide guidance for the job interview for C/C ++ programmers, but to analyze the connotation of the interview questions technically. Most of the interview questions in this article come from major forums. Some answers to the questions also refer to the comments of netizens.

Many interview questions seem simple, but they require profound basic skills to provide 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. In addition, there are some interview questions to examine the examinee's agile thinking ability.

The analysis of these interview questions is very interesting. As a R & D personnel, the in-depth analysis of these interview questions can further enhance their internal strength.

2. Locate 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 = 'a ';
}
Strcpy (string, str1 );
}
Question 3:

Void test3 (char * str1)
{
Char string [10];
If (strlen (str1) <= 10)
{
Strcpy (string, str1 );
}
}
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

Void strcpy (char * strDest, char * strSrc)
{
While (* strDest ++ = * strSrc ++ )! = '\ 0 ');
}
4 points

Void strcpy (char * strDest, const char * strSrc)
// Add the source string to const, indicating that it is an input parameter and adds 2 points
{
While (* strDest ++ = * strSrc ++ )! = '\ 0 ');
}
7 points

Void strcpy (char * strDest, const char * strSrc)
{
// Add non-0 assertions to the source and target addresses, and add 3 points
Assert (strDest! = NULL) & (strSrc! = NULL ));
While (* strDest ++ = * strSrc ++ )! = '\ 0 ');
}
10 points

// For chained operation, add 3 points to return the destination address!

Char * strcpy (char * strDest, const char * strSrc)
{
Assert (strDest! = NULL) & (strSrc! = NULL ));
Char * address = strDest;
While (* strDest ++ = * strSrc ++ )! = '\ 0 ');
Return address;
}
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.

{
Assert (strt! = NULL); // The asserted string address is not 0
Int len;
While (* str ++ )! = '\ 0 ')
{
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 ");
Printf (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 (& str, 100 );
Strcpy (str, "hello ");
Printf (str );
}
Question 7:

Void Test (void)
{
Char * str = (char *) malloc (100 );
Strcpy (str, "hello ");
Free (str );
... // Other omitted statements
}
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 value assignment statement are 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:

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 zone, causing program running to crash. In VC ++, the "Access Violation" error is prompted during DEBUG ". The program should be changed:

Swap (int * p1, int * p2)
{
Int p;
P = * p1;
* P1 = * p2;
* P2 = p;
}
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.