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

Source: Internet
Author: User
C ++ programmers apply for common interview questions in-depth analysis 2 -- Linux general technology-Linux technology and application information, the following is to read details. 3. Internal issues

Question 1: Give the if statement for comparing the BOOL, int, float, pointer variable and "zero value" respectively (assuming the variable name is var)

Answer:

Boolean variable: if (! Var)

Int type variable: if (var = 0)

Float variables:

Const float EPSINON = 0.00001;

If (x> =-EPSINON) & (x <= EPSINON)

Pointer variable: if (var = NULL)

Analysis:

Evaluate the internal function of the 0 value. if (var = 0) can be used to determine the 0 value of the BOOL variable, and if (! Var), pointer variable judgment can also be written as if (! Var), although the program can run correctly, it cannot clearly express the meaning of the program.
Generally, if you want if to determine whether a variable is "true" or "false", you should directly use if (var), if (! Var), indicating that it is a "logical" judgment. if you use if to judge a numeric variable (short, int, long, etc.), you should use if (var = 0 ), it indicates that it is compared with 0 on the "value", and the judgment pointer is suitable to use if (var = NULL), which is a good programming habit.

Float variables are not accurate, so do not use "=" or "! = "To the number, you should try to convert it into the form of"> = "or" <=. If it is written as if (x = 0.0), an error is returned. The score is 0.

Question 2: The following is a 32-bit C ++ program under Windows NT. Calculate the sizeof value.

Void Func (char str [1, 100])
{
Sizeof (str) =?
}

Void * p = malloc (100 );
Sizeof (p) =?
Answer:

Sizeof (str) = 4
Sizeof (p) = 4
Analysis:

When the array name in the Func (char str [100]) function acts as a function parameter, In the function body, the array name loses its meaning and is just a pointer; while losing its meaning, it also loses its constant feature. It can perform auto-increment, auto-subtraction, and other operations and can be modified.

The essence of array names is as follows:

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

For example:

Char str [10];
Cout <sizeof (str) <endl;
The output is 10. str indicates the data structure char [10].

(2) The array name can be converted to a pointer pointing to the object. It is a pointer constant and cannot be used for auto-increment, auto-subtraction, or other operations;

Char str [10];
Str ++; // compilation error, prompting that str is not the left Value
(3) When the array name is used as a function parameter, it becomes a common pointer.

On Windows NT 32-bit platform, the pointer length (memory usage) is 4 bytes, so sizeof (str) and sizeof (p) are 4.

Question 3: write a "standard" macro MIN, which inputs two parameters and returns a smaller one. What will happen when you write the following code?

Least = MIN (* p ++, B );
Answer:

# Define MIN (A, B) (A) <= (B )? (A): (B ))
MIN (* p ++, B) produces macro side effects

Analysis:

This interview mainly examines the use of the macro definition. The macro definition can implement functions similar to the function, but it is not a function, and the "parameter" in the arc of the macro definition is not a real parameter, during macro expansion, "Parameters" were replaced one to one.

Programmers should be very careful with the use of macro definitions, and pay special attention to two problems:

(1) carefully enclose the "parameter" in the macro definition and the entire macro with an arc. Therefore, strictly speaking, the following answers:

# Define MIN (A, B) (A) <= (B )? (A): (B)
# Define MIN (A, B) (A <= B? A: B)
Both should be set to 0 points;

(2) Prevent the side effects of macros.

Macro definition # define MIN (A, B) (A) <= (B )? (A): (B) the effect on MIN (* p ++, B) is as follows:

(* P ++) <= (B )? (* P ++): (* p ++ ))

This expression produces side effects. The pointer p performs three ++ auto-increment operations.

In addition, the other answer that should be set to 0 is:

# Define MIN (A, B) (A) <= (B )? (A): (B ));
This solution adds ";" behind the macro definition, showing that the author's macro concept is vague and can only be ruthlessly scored 0 points and eliminated by the interviewer.

Question 4: Why do standard header files have a structure similar to the following?

# Ifndef _ INCvxWorksh
# Define _ INCvxWorksh
# Ifdef _ cplusplus

Extern "C "{
# Endif
/*...*/
# Ifdef _ cplusplus
}

# Endif
# Endif/* _ INCvxWorksh */
Answer:

Compile macro In header file

# Ifndef _ INCvxWorksh
# Define _ INCvxWorksh
# Endif
To prevent repeated references.

As an object-oriented language, C ++ supports function overloading, while Procedural Language C does not. After the function is compiled by C ++, the name in the symbol library is different from that in the C language. For example, assume that the prototype of a function is:

Void foo (int x, int y );
After the function is compiled by the C compiler, the name in the symbol library is _ foo, while the C ++ compiler generates names such as _ foo_int_int. The names such as _ foo_int_int contain the function name and the number and type of function parameters. C ++ uses this mechanism to implement function overloading.

In order to realize the mixed programming of C and C ++, C ++ provides the C connection to exchange the specified symbol extern "C" to solve the name matching problem, after the extern "C" is added before the function declaration, the compiler will compile the function as _ foo in the C language, so that the C ++ function can be called in the C language.
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.