Some difficult questions about C ++

Source: Internet
Author: User

1. How is map implemented internally in STL?
A: red/black trees.
Binary Trees work most effectively when they are balanced or when the height from the leaf node to the root node is within a certain range. The red-black tree algorithm is an algorithm of the Balance Tree. This name is because every node of the tree is red or black, and the color of the node is used to detect the balance of the tree. During the insert and delete operations on the node, it may be rotated to maintain the tree balance. Average and worst case insertion, deletion, and search time are both O (lg n ). For details, see cormen [2001].
Theory
A red/black tree is a binary search tree with the following attributes:
1. All nodes are colored in red or black.
2. Each leaf node is empty and black.
3. If the parent node is red, the two child nodes are black.
4. Each simple path from a node to its child node contains the same number of black nodes.
5. The root node is always black.

2. How are objects stored in the memory?
A: Read <inside the C ++ object model>.
Simply put,
In the memory layout of class objects, if there is a virtual function, first the vtbl pointer of the class, and then the object data, the object data is stored in sequence.

Of course, it will involve byte alignment, which will improve the access efficiency.

3. How does com work?
A: COM, in short, is a way for different applications and languages to share binary code. Unlike C ++, it is only source code-Level Reuse. Windows allows you to use DLL for Binary-level code sharing, such as kernel32.dll and user32.dll. However, all these DLL files are written in C, therefore, they can only be called by C or a language that understands the C call method. MFC introduces another binary-level code sharing mechanism-MFC extension DLLs, but this mechanism limits more, you can only use them in the MFC program. Com solves these problems by establishing a binary specification, which means that the COM binary module should be organized according to a special structure, and the problem is also solved in the memory. The rule is language-independent and is handed over to the compiler.

The organizational structure of COM objects in the memory is the same as that of C ++ virtual functions. This is why most COM Code uses C ++, but remember that com is indeed language-independent, because the generated result code can be used by all other languages. By the way, COM is not a Win32 specification. Theoretically, it can be transplanted to Unix and any other operating systems, but I have never seen COM.

4. Why use smart pointers? How is it implemented?
To avoid resource leakage.

The reference counting mechanism is used internally, and the specific implementation is very complicated.

5. I will give you a pointer and use new to dynamically apply for space and release it in another function. I don't know whether to apply for an element or an array. How can I determine whether to use delete or delete []?
Different compilers have different implementation mechanisms. There are two commonly used methods:
1. When new, the number of objects allocated is recorded before the first object.
2. Use the key value, that is, key-Value
For ex:
Int * P = new int [N];
P is key and N is value.

6. How are virtual functions implemented?
A: The virtual function table is used.

7. How is the virtual function table implemented?
A:
Each class containing virtual functions has a virtual function table (vtbl). Each item in the table points to the address of a virtual function and is implemented as an array of function pointers.

The virtual function table has both inheritance and polymorphism. The vtbl of each derived class inherits the vtbl of each base class. If the base class vtbl contains one item, the vtbl of the derived class also contains the same item, however, the values of the two items may be different. If the derived class overwrites the virtual function corresponding to this item, the value of vtbl In the derived class points to the overloaded virtual function. If this function is not overloaded, the base class value is used.

In the memory layout of class objects, the vtbl pointer of the class is first followed by the object data. When a virtual function is called through an object pointer, the code generated by the compiler first obtains the vtbl pointer of the object class, and then calls the corresponding item in vtbl. When an object pointer is called, you cannot determine whether the pointer points to a base class object, a derived class object, or a derived class object during compilation. However, when the call statement is executed during running, it is determined that the compiled call code can obtain the correct vtbl based on the specific object and call the correct virtual function to realize polymorphism.

The essence of the problem is that the object pointer that calls a virtual function lacks more information during compilation, however, there is enough information during the operation, but it is no longer bound at that time. How can we make a transition between the two? Record the information required for binding with a common data structure, which can be associated with object pointers. during compilation, you only need to use this data structure for abstract binding, during the running, you will get a real binding. The data structure is vtbl. As you can see, the abstraction and polymorphism required for implementing the user need to be bound later, while the compiler implements post-binding through abstraction and polymorphism.

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.