C/C ++ internal questions for programmers

Source: Internet
Author: User

Note: Some of the following content references the Internet and cannot find the source. However, I have carefully read it. If you find any error or deficiency, please correct or supplement it. Thank you!

 

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 variation (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 ++): (B ))

This expression produces side effects, and the pointer P performs two ++ 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, its name in the symbol library is _ Foo, 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.

 

Question 5: Tell me as many static and const keywords as possible

Answer:

The static keyword has at least N functions:

(1) The static variable in the function body applies to this function body. Unlike the auto variable, the memory of this variable is allocated only once, therefore, the value remains the value of the previous time during the next call;

(2) The static global variables in the module can be accessed by the functions used in the module, but cannot be accessed by other functions outside the module;

(3) The static function in the module can only be called by other functions in the module. The scope of use of this function is limited to the module that declares it;

(4) static member variables in the class belong to the entire class and only one copy of all objects in the class;

(5) The static member function in the class belongs to the whole class. This function does not receive the this pointer, so it can only be a static member variable of the category.

The const keyword has at least N functions:

(1) to prevent a variable from being changed, you can use the const keyword. When defining the const variable, you usually need to initialize it, because there will be no chance to change it in the future;

(2) For pointers, you can specify the pointer itself as const, or you can specify the data referred to by the pointer As const, or both of them as const;

(3) In a function declaration, const can modify the form parameter, indicating that it is an input parameter and cannot change its value within the function;

(4) If the member function of the class is specified as the const type, it indicates that it is a constant function and the member variables of the class cannot be modified;

(5) For a member function of the class, you must specify its return value as the const type so that its return value is not the "Left value ". _______ Example:

Const classa operator * (const classa & A1, const classa & A2 );

The return result of operator * must be a const object. If not, such abnormal code will not cause compilation errors:

Classa A, B, C;

(A * B) = C; // assign a value to the result of a * B

Operation (A * B) = C obviously does not conform to the programmer's original intention, and does not make any sense.

Analysis:

Surprised? What are the functions of small static and const? If you can only answer 1 ~ 2. You have to close the door and practice well. This question can be used to check whether the subject's knowledge about programming is elementary, intermediate, or in-depth. Without a certain knowledge breadth and depth, it is impossible to give a comprehensive answer to this question. Most people can only answer some functions of the static and const keywords.

 

Question 6: your opinion on the wild pointer

Answer:

In either of the following situations, the wild pointer is displayed:

(1) When the memory to which the Pointer Points is recycled, the pointer value is not set to null, for example:

Int * P = new int [10];

'''

Deletp [] P;

// P = NULL; // P = NULL should be added here; prevent subsequent code from continuing to reference P

(2) the pointer points to a new memory space and does not release the original memory space, for example:

Int * P = new int;

Int M = 1;

P = & M; // the memory space referred to by P should be released here; otherwise, the memory space originally referred to by P will not be released.

How to eliminate wild pointers:

(1) Delete a dynamic object and set it to null in time. Before removing the reference from the pointer, determine whether the pointer is null;

(2) do not assign local pointer in a function and send it to another function Delete;

(3) Try to use smart pointers in C ++.

 

........................ (To be continued)

 

 

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.