20230920 R&D review

1.Virtual functions and virtual function tables in cpp

The main function of virtual functions in C++ is to implement the polymorphism mechanism. Regarding polymorphism, in short, it means using the parent class pointer to point to the instance of its subclass, and then calling the member function of the actual subclass through the parent class pointer.

The virtual function table refers to an array of function addresses that exists in eachclass that contains a virtual function. When we use the pointer of the parent class to operate a subclass, this virtual function table indicates the actual function that should be called.

The C++ compiler guarantees that the pointer to the virtual function table exists at the front of the object instance, so that it can be obtained through the address of the object instance This virtual function table can then traverse the function pointers in it and call the corresponding function.

Single inheritance: 1. Virtual function subclass overrides the parent class 2. The parent class is in front of the subclass
Multiple inheritance: 1. Place in the order of declaration 2. Virtual function subclass overrides Parent class

Insert image description here
Explanation:
Only when the parent class and subclass have virtual functions with the "same" prototype can the parent class or subclass be called based on the type of object pointed to by the parent class pointer. Virtual functions exhibit polymorphism. If the parent class has virtual functions but the subclass does not, or vice versa, it cannot exhibit polymorphic behavior. When a function is called with a parent class pointer, no matter whether the function is a virtual function or not, it must exist in the parent class to which the pointer belongs, otherwise the compiler will definitely report an error because it cannot find the function

2.The difference between new and malloc

Reference https://www.cnblogs.com/qg-whz/p/5140930.html
Insert image description here

3.The difference between overload, override and overwrite

Insert image description here

4.shared_ptr and weak_ptr; shared_ptr circular reference problem and solution

In C++, it is often necessary to create a new object, open up a memory space, and return a pointer to operate this memory. After use, you need to release the memory space through delete. If the memory is not released, this memory cannot be reused, resulting in a memory leak. In order to reduce human negligence, three new smart pointers are introduced in C++ 11 to automatically manage memory resources.

Insert image description here
Shared_ptr is implemented based on the "reference counting" model. Multiple shared_ptr can point to the same dynamic object, and a shared reference counter is maintained to record the number of shared_ptr instances that reference the same object. When the last shared_ptr pointing to a dynamic object is destroyed, the object it refers to will be automatically destroyed (through the delete operator)

weak_ptr is used to solve the circular dependency problem of the "reference counting" model. weak_ptr points to an object and does not increase or decrease the reference counter of the object.

A strong reference exists while the referenced object is alive.

Weak reference does not necessarily exist when the referenced object is alive. Just a reference when it exists. A weak reference does not modify the reference technology of the object, which means that the weak reference does not manage the object's memory and is similar in function to an ordinary pointer. However, a big difference is: Weak references can detect whether the managed object has been released, thereby avoiding access to illegal memory

The object's memory management is performed by the strongly referenced shared_ptr. weak_ptr only provides a means of access to managed objects. But this method can only be used when the programmer can foresee the occurrence of circular references. It can also be said that this is just a compile-time solution.

5. The difference between process stack and heap in cpp

Insert image description here
Application method: stack system allocation; heap programmers set the size malloc new
Apply for resp: stack remaining space is greater than the applied space; for heap, OS has a linked list that records free memory. Find the first heap node larger than the space, record the actual size, and put the rest back into the linked list
Application size: stack downward, continuous space, windows is 2M, there will be overflow; heap Upward discontinuity, limited by virtual memory
Application efficiency: The stack is fast, and the heap is easily fragmented when it is full
Storage location: The stack first enters the first instruction, and then Each parameter (from right to left), then local variables; heap record size, then specific content;
The stack is provided by the system and is supported by special registers and machine instructions
Heap is provided by the function library, but it is more flexible

6. Use vector in cpp to insert data

The two member functions insert() and emplace() are used to insert elements at the specified position of the container
The function of the insert() function is to insert elements at the specified position of the vector container. Insert one or more elements. There are many syntax formats for this function
emplace is used to insert a new element before the specified position in the vector container
A simple understanding is that emplace() is inserting When creating an element, the element is constructed directly at the specified location in the container, rather than being generated separately and then copied (or moved) to the container. Therefore, in actual use, it is recommended that you give priority to emplace().

7.The difference between unordered_map and map

map: A red-black tree is implemented inside the map. This structure has the function of automatic sorting, so all elements inside the map are ordered. Each node of the red-black tree represents an element of the map. , Therefore, a series of operations such as search, deletion, and addition for map are equivalent to performing such operations on the red-black tree. Therefore, the efficiency of the red-black tree determines the efficiency of the map.
unordered_map: unordered_map implements a hash table internally, so the order of its elements is messy and unordered
Insert image description here

8.LRU algorithm

The LRU (Least recently used) algorithm eliminates data based on the historical access records of the data. Its core idea is that "if the data has been accessed recently, the probability of being accessed in the future is also higher."

New data is inserted into the head of the linked list;
Whenever the cache hits (that is, the cached data is accessed), the data is moved to the head of the linked list;
When the linked list is full, discard the data at the end of the linked list.

9. Red-black trees and balanced binary trees, rotation of red-black trees

Balanced binary trees, also known as AVL trees, were born to solve the problem that binary trees degenerate into a linked list.

Features of balanced binary tree:

has all the characteristics of a binary search tree.
The height difference between the left subtree and the right subtree of each node is at most equal to 1.

For scenarios where frequent deletions and insertions occur, the adjustment process of the balanced binary tree obviously has performance problems, so in order to solve this problem, red-black trees were introduced.

Characteristics of red-black trees:

has all the characteristics of a binary tree.
Each node can only be red or black.
The root node can only be black, and the black root node does not store data.
No adjacent node can be red at the same time.
For a red node, its child nodes can only be black.
All paths from any node to each of its leaves contain the same number of black nodes

The red-black tree does not pursue absolute balance due to operations such as insertion and deletion. It has a small number of rotations, with a maximum of two rotations for insertion and a maximum of three rotations for deletion. Therefore, when there are many search, insertion, and deletion operations, the red-black tree The efficiency is better than the balanced binary tree.

10.Why is cpp faster than py?

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_40986490/article/details/133071256