[Collection] classical C/C ++ interview questions (5)

Source: Internet
Author: User

1. What is the difference between the C ++ class and the struct in C?
The default access permission of a struct member is public, while that of a class member is private.

2. Use and function of destructor and virtual functions
The Destructor is a function automatically called at the end of the object's lifetime to release the memory allocated by the constructor.
A virtual function is a function specified by the keyword Virtual. It is used for C ++ polymorphism.

3. What is the difference between global variables and local variables? How is it implemented? How does the operating system and compiler know?
1) The role of global variablesProgramAnd local variables act on the current function.
2) The former is allocated in the memory in the global data zone, and the latter is allocated in the stack zone.
3) different lifecycles: global variables are created and created along with the main program and destroyed along with the destruction of the main program. Local variables exist inside the local function or even within the local cyclic body. Exit does not exist.
4) usage is different: After Declaration, all parts of the global variable program can be used, and local variables can only be used locally.

4. There are n natural numbers (1--n) of varying sizes. please sort them from small to large. Requires the programAlgorithm: The time complexity is O (n), and the space complexity is O (1 ).
Void sort (int e [], int N)
{
Int I;
Int t;/* Temporary Variable: space complexity O (1 )*/
For (I = 1; I <n + 1; I ++)/* time complexity O (n )*/
{
T = E [E [I];/* The element with the subscript E [I]. After sorting, its value is E [I] */
E [E [I] = E [I];
E [I] = T;
}
}

5. Difference Between Stack and stack
A. Different Application Methods
Stack is automatically allocated by the system, and heap needs to be applied by the programmer and specify the size.
B. Different system responses after application
STACK: The system provides the program with memory as long as the remaining space of the stack exceeds the requested space. Otherwise, an Stack Overflow exception is thrown.
Heap: when the system receives the application, it first traverses the linked list of idle memory addresses recorded in the operating system to find the first heap node larger than the applied space, delete the node from the space node linked list and allocate the space of the node to the program. In addition, most systems will record the size of the allocation at the first address in the memory space, so that the delete statement can correctly release the space. In addition, because the size of the heap node is not necessarily equal to the applied size, the system will automatically re-place the excess part into the idle linked list.
C. Different Application size limits
STACK: in windows, the stack size is 2 MB (OR 1 MB). If the requested space exceeds the remaining space of the stack, overflow will be prompted. Therefore, the space available from the stack is small.
Heap: the heap is a data structure extended to the high address, and is a non-sequential memory area. This is because the system uses the linked list to store the idle memory address, which is naturally discontinuous, And the traversal direction of the linked list is from the low address to the high address. The heap size is limited by the valid virtual memory in the computer system. It can be seen that the space obtained by the heap is flexible and large.
D. Comparison of application efficiency:
The stack is automatically allocated by the system, which is faster. But programmers cannot control it.
Heap is the memory allocated by new. It is generally slow and prone to memory fragments, but it is most convenient to use.
In addition, in windows, the best way is to use virtualalloc to allocate memory. Instead of heap or stack, it is to reserve a fast memory in the address space of the process, although it is the most inconvenient to use. However, it is fast and flexible.
E. Storage content in heap and stack
STACK: when calling a function, the first entry to the stack is the address of the next instruction in the main function (the next executable statement in the function call statement), and then the parameters of the function, in most C compilers, parameters are written from right to left into the stack, followed by local variables in the function. Note that static variables are not included in the stack. When the function call ends, the local variable first goes out of the stack, then the parameter, and the top pointer of the stack points to the address of the initial storage, that is, the next instruction in the main function, where the program continues to run.
Heap: Generally, the heap size is stored in one byte in the heap header. The specific content in the heap is arranged by the programmer.

6. Advantages and Disadvantages of macros and functions with Parameters
MACRO:
Advantage: It is completed in the pre-processing stage, without occupying the compilation time. At the same time, it saves the overhead of function calls and achieves high running efficiency.
Disadvantage: if you do not check the type, multiple macro replacement will result inCodeThe macro is essentially a string replacement, which may lead to incorrect results due to side effects of some parameters.
Function:
Advantage: The type check is performed without any side effects that may be caused by macro parameters, and the correctness of the calculation is ensured.
Disadvantage: function calls require the overhead of the input stack and the output stack of parameters and return addresses, and the efficiency is not high with macro parameters.

PS: differences between macros and inline functions
Inline functions and macros are expanded in the place where the program appears. inline functions are not implemented through function calls. They are expanded (completed during compilation) in the program where the function is called ); macros are the same;
The difference is that inline functions can complete compilation functions such as type detection and statement correctness during compilation; macros do not, in addition, the time for macro expansion is different from that for inline functions (expanded during runtime)

7. Where is the entry to the Windows program? Process for writing out windows message mechanism
the entry of Windows program is the winmain () function.
Message Processing Mechanism for Windows applications:
. the operating system receives the window message of the application and delivers the message to the Message Queue of the application.
B. the application calls the getmessage function in the message loop to retrieve a message from the message queue. After the message is retrieved, the application can pre-process the message.
C. The application calls dispatchmessage to send the message back to the operating system.
D. The system uses the pointer of the window process function stored by the lpfnwndproc Member of the wndclass struct to call the Window Process and process the message.

8. How to define and implement a class member function as a callback function
A. What is a callback function?
In short, the callback function is called by the caller.
When a callback function is called (usually an API function), the address of one of its own functions (this function is a callback function) is passed as a parameter to the called function. When needed, the called function calls the callback function using the passed address.
The callback function is written by yourself. You need to call another function. One of the parameters of this function is your callback function name. In this way, the system will call the callback function you write when necessary, so that you can complete what you want to do in the callback function.

B. How to define and implement a class member function as a callback function
To define and implement a class member function as a callback function, you need to do three things:
A. statement;
B. definition;
C. Set the trigger condition, that is, use your callback function name as a parameter in your function for the system to call
For example:
I. Declare the callback function type
Typedef void (* funptr) (void );
Ii. Define callback Functions
Class
{
Public:
A ();
Static void callbackfun (void) // callback function, which must be declared as static
{
Cout <"callbackfun" <Endl;
}
Virtual ~ A ();
};

3. Set the trigger condition
void funtype (funptr p)
{< br> P ();
}< br> void main (void)
{< br> funtype (A: callbackfun);
}

C. Callback functions and API functions
The callback API is very similar. They all use cross-layer called functions. However, the difference is that the API is provided by the lower layer to the upper layer. Generally, this function is known to the upper layer. The callback is the opposite. It is provided by the upper layer to the lower layer, it is unknown to the lower layer and must be installed by the upper layer. This installation function is actually an API provided by the lower layer. After the installation, the lower layer does not know the name of this callback, however, it uses a function pointer to save the callback function. To call this function, you only need to reference this function pointer and related parameter pointers.

In fact, the callback function is written in the upper layer. The lower layer stores the function through a function pointer. When an event is triggered, the lower layer calls the upper layer function through the function pointer.

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.