An in-depth analysis of common face test questions by C + + programmers

Source: Internet
Author: User
Tags assert definition bool comparison constant printf reference strlen

1. Introduction

The purpose of this paper is not to provide C + + Programmer job interview guidance, but to analyze the content of the interview question from the technical point of view. Most of the papers in the paper from the major forums, some of the questions answered also refer to the views of netizens.

Many face questions seem simple, but need a deep basic skills to give a perfect answer. Enterprises require the interviewer to write a simple strcpy function can be seen in the technical level of the interviewer, 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 example of the strcpy function from 2 to 10 points to see what level they belong to. In addition, there are some test questions to examine the interviewer's ability to think quickly.

Analysis of these questions, itself contains a very strong interest, and as a researcher, through the in-depth analysis of these questions can further enhance their own internal strength.

2. Finding 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 ) <= 10 )
 {
  strcpy( string, str1 );
 }
}

Answer:

The question 1 string str1 requires 11 bytes to be stored (including the ' "At the End"), while the string has only 10 bytes of space, and the strcpy can cause the array to go out of bounds;

For question 2, if the interviewer points out that the character array str1 cannot end within the array, it can give 3 points, and if the interviewer points out that strcpy (string, str1) call makes the number of bytes replicated from STR1 memory to string memory an uncertainty can give 7 points, On this basis, it is pointed out that the strcpy of library function is 10 points.

The question 3,if (strlen (str1) <= 10) should be changed to if (Strlen (STR1) < 10), because the result of the strlen does not count the 1 bytes occupied by ' the '.

Analysis:

Examine the mastery of basic skills:

(1) The string ends with ';

(2) The sensitivity of the array to be grasped;

(3) Library function strcpy work, if you write a standard strcpy function of the total score of 10, the following gives a few different points of the answer:

2 points

void strcpy( char *strDest, char *strSrc )
{
  while( (*strDest++ = * strSrc++) != ‘\0’ );
}

4 points

void strcpy( char *strDest, const char *strSrc )
//将源字符串加const,表明其为输入参数,加2分
{
  while( (*strDest++ = * strSrc++) != ‘\0’ );
}

7 points

void strcpy(char *strDest, const char *strSrc)
{
 //对源地址和目的地址加非0断言,加3分
 assert( (strDest != NULL) && (strSrc != NULL) );
 while( (*strDest++ = * strSrc++) != ‘\0’ );
}

10 points

//为了实现链式操作,将目的地址返回,加3分!
char * strcpy( char *strDest, const char *strSrc )
{
 assert( (strDest != NULL) && (strSrc != NULL) );
 char *address = strDest;
 while( (*strDest++ = * strSrc++) != ‘\0’ );
  return address;
}

From 2 points to 10 points of several answers we can clearly see that 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 the strlen, it does not include the ' "" at the end of the string.

Readers read the strcpy version of the different points, 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 ); //断言字符串地址非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 );
 ... //省略的其它语句
}

Answer:

Question 4 Incoming the getmemory (char *p) function's parameter is a string pointer, and modifying the parameter inside the function does not really change the value of the incoming parameter, execution

char *str = NULL;
GetMemory( str );

The posterior str is still null;

in question 5

char p[] = "hello world";
return p;

P[] Array is a local automatic variable within a function, and memory has been freed after the function returns. This is a common mistake many programmers make, the root of which is the failure to understand the lifetime of a variable.

The getmemory in question 6 avoids question 4, the getmemory parameter is a pointer to the string pointer, but the application memory and assignment statements are executed in the GetMemory

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

After not judging whether the memory is successful or not, add:

if ( *p == NULL )
{
 ...//进行申请内存失败处理
}

Question 7 exists the same problem as the Test 6 in the implementation

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

The judgment that the memory is not applied for success is not followed; In addition, no str is empty after free (str), resulting in the possibility of becoming a "wild" pointer, plus:

str = NULL;

The malloc memory is also not released in the test function of question 6.

Analysis:

The test questions 4~7 examines the interviewer to the memory operation understanding degree, the basic skill solid interviewer generally can answer correctly the 50~60 mistake. But to be perfectly correct, it is no easy job.

The examination of memory operations focuses on:

(1) The understanding of the pointer;

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

(3) Good dynamic memory application and release habits.

Let's take a look at what's wrong with the following procedure:

swap( int* p1,int* p2 )
{
 int *p;
 *p = *p1;
 *p1 = *p2;
 *p2 = *p;
}

In the Swap function, p is a "wild" pointer that may point to the system area, causing the program to run a crash. In VC + + Debug runtime prompts for error "Access violation". The procedure should read:

swap( int* p1,int* p2 )
{
 int p;
 p = *p1;
 *p1 = *p2;
 *p2 = p;
}

3. Internal problems

Question 1: Give the bool,int,float, the pointer variable and "0 value" comparison of the IF statement (assuming variable name is VAR)

Answer:

BOOL type variable: if (!var)

int type variable: if (var==0)

Float type variable:

const float Epsinon = 0.00001;

if ((x >=-Epsinon) && (x <= Epsinon)

Pointer variable: if (var==null)

Analysis:

Examination of the "internal strength" of the 0 value judgment, the 0 judgment of the bool variable can be written as if (var==0), and the int variable can also be written as if (!var), the judgment of the pointer variable can also be written as if (!var), although the procedure can run correctly, But the meaning of the procedure was not clearly expressed.

In general, if you want to let if judge a variable's "true", "false", you should use if (Var), if (!var) directly to indicate that it is "logical" judgment, if you use if to judge a numeric variable (short, int, long, etc.), should use if (var==0), It is a good programming habit to show that 0 is a "numerical" comparison, and that it is appropriate to use if (var==null) for the judgment pointer.

Floating-point variables are not accurate, so float variables cannot be used with "= =" or "!" = "In contrast to numbers, it should be managed to be converted into" >= "or" <= "forms. If written as if (x = = 0.0), the sentence is wrong, 0 points.

Question 2: The following is a 32-bit C + + program under Windows NT, please calculate the value of sizeof

void Func ( char str[100] )
{
 sizeof( str ) = ?
}
void *p = malloc( 100 );
sizeof ( p ) = ?

Answer:

sizeof( str ) = 4
sizeof ( p ) = 4

Analysis:

Func (char str[100]) function, the array name is used as a function parameter, in the function body, the array name loses its connotation, is only a pointer, loses its intension at the same time, it also loses its constant characteristic, may make the increment, the self reduction and so on the operation, may be modified.

The nature of the array name is as follows:

(1) The array name refers to a data structure, which is an array;

For example:

char str[10];
cout << sizeof(str) << endl;

The output result is 10,STR reference data structure CHAR[10].

(2) The array name can be converted to a pointer to its reference entity, and it is a pointer constant, which cannot be modified because it cannot be used for self increment and decrement.

char str[10];
str++; //编译出错,提示str不是左值 

(3) When an array name is used as a function parameter, it is reduced to a normal pointer.

Under Windows NT 32-bit platform, the length of the pointer (memory size) is 4 bytes, so sizeof (str), sizeof (p) are 4.

Question 3: Write a "standard" macro min, this macro enters two parameters and returns the smaller one. In addition, what happens when you write the following code?

least = MIN(*p++, b);

Answer:

#define MIN(A,B) ((A) <= (B) ? (A) : (B))

MIN (*p++, b) can create side effects of macros

Analysis:

This question mainly examines the interviewer's use of the macro definition, a macro definition can implement functions similar to a function, but it is not a function at all, and the "arguments" in parentheses in a macro definition are not real arguments, and a one-to-one substitution is made for "parameters" when the macro is expanded.

Programmers should be very careful about using macro definitions, with particular attention to two issues:

(1) Carefully enclose the "parameters" and the entire macro in the macro definition in parentheses. So, strictly speaking, the following answers:

#define MIN(A,B) (A) <= (B) ? (A) : (B)
#define MIN(A,B) (A <= B ? A : B )

Should be sentenced to 0 points;

(2) To prevent the macro side effects.

Macro definition #define MIN (a,b) ((A) <= (B)? (A): (b) The result of the effect on min (*p++, b) is:

((*p++) <= (b)? (*p++): (*p++))

This expression will have side effects, and the pointer P will do three + + self-increment operations.

In addition, another answer that should be 0 points is:

#define MIN(A,B) ((A) <= (B) ? (A) : (B));

This solution adds ";" to the macro definition, showing that the creator's concept of the macro is ambiguous and can only be ruthlessly sentenced to 0 points and eliminated by the interviewer.

Question 4: Why does the standard header file have a structure similar to the following?

#ifndef __INCvxWorksh
#define __INCvxWorksh
#ifdef __cplusplus
extern "C" {
#endif
/*...*/
#ifdef __cplusplus
}
#endif
#endif /* __INCvxWorksh */

Answer:

Compiled macros in header files

#ifndef __INCvxWorksh
#define __INCvxWorksh
#endif

The role is to prevent repeated references.

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.