C + + Classic Face question (ii)

Source: Internet
Author: User

Recently read some interview questions, feel that if they are asked, and can not be very neat answer.

One is never this consciousness, two is not carefully combed under.

The following analysis of these questions, ha! Personal ability is limited, which inevitably have omissions, welcome to add!


11. What is the difference between a reference and a pointer?
"Reference Answer"
1) The reference must be initialized, and the pointer does not have to.
2) The reference initialization cannot be changed and the pointer can change the object being referred to.
3) There is no reference to a null value, but there is a pointer to a null value.


can refer to the following in the 5 questions to supplement the content




What are the meanings of virtual and inline in 12.c++?
"Reference Answer"
The addition of the virtual keyword before the declaration of a base class member function means that the member function is declared as a virtual function.
Inline is put together with the definition of the function, making the function known as inline.
Inline is a keyword for implementation, not a keyword for declaration. 
The characteristics of virtual functions; If you want a derived class to be able to redefine a method of a base class, define the method as a virtual method in the base class, which enables dynamic linking. 
The characteristics of an inline function, and the purpose of using an inline function is to improve the efficiency of function operation. The code for the inline function body cannot be too long, because the inline function eliminates the time it takes to call the function at the expense of code bloat. Inline functions cannot contain loop statements because it is more expensive to execute a loop statement than to call a function.


Ha! I think I really need to pay attention to the ability to express.




13.
VC, what does the debug and release options within the compilation toolbar mean?
"Reference Answer"
Debug is often called a debug version, it contains debugging information, and does not make any optimizations to facilitate the programmer to debug the program.
Release, known as the release version, is often optimized to make the program optimal in terms of code size and speed, so users can use it well.


Debug with a lot of debugging code, the runtime needs the corresponding runtime library,
Release Mode program Compact does not contain debug code and information, can be run directly (if you do not need to run the library)




14. What is the use of function assert?
The "reference answer" assertion assert is a macro that works only in the debug version and is used to check what should not happen. Programmers can think of assert as a harmless test method that can be safely used in any system state


Learn VC when I saw the Assert macro definition, but I never used ...




15.const comparison with #define, what are the advantages of const?
"Reference Answer"
(1) const constants have data types, and macro constants do not have data types. The compiler can perform type safety checks on the former. Instead of only character substitution, there is no type safety check, and the substitution of characters can produce unexpected errors (marginal effects).
#define不能生成 the exclusive constant for the class, because it simply replaces it:
(2) Some integrated debugging tools can debug const constants, but cannot debug macro constants.


The effective C + + recommends replacing # define with Const, enum, and inline.
Reasons to use enum
In some cases, the compiler does not allow static constants to appear in the initial settings of the class, only in the form of an enum
On the other hand, enum and # define are more similar, none of them have specific types, for example, there is a const int A; There is no #define N int A,enum and # define are not able to be addressed, while Const can.




16. Please talk about the differences between references and pointers.
"Reference Answer"
(1) The reference is created and must be initialized (the pointer can be initialized at any time).
(2) cannot have a null reference, the reference must be associated with a valid storage unit (the pointer can be null).
(3) Once a reference is initialized, the referenced relationship cannot be changed (the pointer can change the object at any time).


Constantly appearing, it can be seen that this problem is often easy to ask.




17.
Why do you want to new/delete with Malloc/free?
"Reference Answer"
malloc and free are standard library functions for c++/c languages, and new/delete are operators of C + +. They can all be used to request dynamic memory and free memory.
For objects of non-intrinsic data types, light Malloc/free cannot meet the requirements of dynamic objects.
Objects are automatically executed when they are created, and the object executes the destructor automatically before it dies.
Because Malloc/free is a library function and not an operator, the task of executing constructors and destructors cannot be imposed on malloc/free, not within the control of the compiler.
Therefore, the C + + language requires an operator new that can perform dynamic memory allocation and initialization, and an operator delete that can perform cleanup and deallocation work. Note New/delete is not a library function.


Previously just used to use C when using Malloc/free, with C + + when using New/delete, did not notice these differences.




18. If the memory block is not large enough to be found when dynamic memory is requested, malloc and new will return a NULL pointer declaring the memory request to fail. How do you handle running out of memory?
"Reference Answer"
(1) Determine if the pointer is NULL, and if so, terminate the function with the return statement immediately.
(2) Determine if the pointer is NULL, and if it is, use Exit (1) to terminate the entire program operation immediately.
(3) Set exception handling functions for new and malloc. For example, Visual C + + can use the _set_new_hander function to set user-defined exception handlers for new, or allow malloc to enjoy the same exception handler as new.


I usually return directly.




is 19.c++ type safe?
"Reference answer" is not. Between two different types of pointers can be cast (cast with reinterpret).




const symbolic constants;
(1) const char *P
(2) Char const *P (
3) char * const P
Explain the differences between the above three descriptions;
"Reference Answer"
(1) P is a pointer to the const char, p can be changed to point to, but the value of P point is not changed;
(2) P points to a normal pointer to a const char;
(3) P is a pointer to a const pointer to char.
The definition of (1) and (2) is the same.


The difference between 1, 2, and 3 is that the Const 1 and 2 appear on the left side of the * number, and 3 appear to the right of the *. It seems to have been seen in the words "C and the hands".


Not finished ... To be continued (*^_v_^*)



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Classic Face question (ii)

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.