The way to learn C / C + (1) interview questions and answers at the back end of school recruitment

Time:2021-7-2

1. Structure alignment

Data is usually stored in a storage unit as an integral multiple of 8 bytes in a computer, while various data types contained in the structure are not the same length, and the total size of the structure may be 5 bytes or 7 bytes, etc. The computer can only access the data of integer storage units at a time, so the computer has to access the data of 5 bytes or 8 bytes twice. To sum up, structure alignment can facilitate the computer to access data and reduce the I / O pressure.
How to calculate the size of aligned structures? Use the following two rules to determine: (1) the size of the front unit must be an integral multiple of the size of the back unit, if not, make up.

(2) The size of the entire structure must be an integer multiple of the largest byte
Related knowledge: principles of Computer Organization
2. Explanation of static keyword
(1) In C language

The value of static variable defined inside the function will not disappear after the function is called, but will be retained. Static defined outside the function is only visible inside the file.
  Explanation: the local variables and function parameters inside the function are placed in the system stack. With the end of the call, the data in the stack will disappear, so the value of the local variables inside the function will disappear with the end of the function call. However, static variables are placed in the static area (global variables are also placed here) and will not be discarded at the end of the function call.

(2) In C + +

The main functions of static are (1) hiding (functions / variables are available) (2) ensuring the lifetime of variables (memory function and global survivability) (3) initializing to 0 by default.
  (4) Static global variables will not be used by external files (i.e. privatization), but ordinary global variables can be used by external files.
  In particular, the static keyword is used for the class in C + +, which has the following characteristics:
  (1) Static member functions belong to a class, not to an object.
  (2) Because (1), static member functions have no this pointer, so they can only access static member functions and static member variables. Conversely, non static member functions can access static member functions and static member variables.
  (3) Static member functions cannot be defined as virtual functions.
  (4) Static can't inherit, virtual can't inherit.
  (5) Static member variables are initialized
  (6) Static does not add extra space-time overhead, and can be easily used for communication between subclasses and parent classes.
  (7) It is initialized outside the class without permission modifier, and the general format is: < data type > < class name >: < static member name > = < Val >

3. The difference between hiding, covering and overloading

Covering: a subclass covers a parent function.
 Features: (1) in subclass and parent class respectively
 (2) The function name and parameters should be the same
 (3) The function of the parent class is a virtual function
 Hidden: subclasses mask functions of the same name of the parent class (independent of the parameter list)
 Characteristics (one of them is enough)
 (1) The subclass has the same name as the parent function, but the parameter list is different
 (2) The parent class has no virtual, and the subclass has the same function name and parameter list
 Overload: the compiler decides which function to call according to the parameter list of several functions with the same name declared in the same access area. Overloaded functions may or may not be modified by virtual.

4. External keyword explanation

(1) Tell the compiler that the module or function is defined in another file.
 (2) When used with "C", it means to translate according to the rules of C

5. Const keyword explanation

Limit a variable not allowed to change, resulting in static effect. In order to save space, the compiler does not allocate memory for const, but puts it in the symbol table to make it a constant existing during compilation. There is no operation to store and read memory, so the efficiency is relatively high. In addition, since const is a constant, some content can be protected.
 Const defines a constant and only gives the corresponding memory address, while # define gives an immediate number. Therefore, const constant has only one copy during program running, while # define definition has several copies in memory.

6. Inline function and macro definition

Inline function is a function declared by the inline keyword, which is a special function of C + +. It is suggested that the compiler should insert the function body directly into the function call instead of calling it through the function call mechanism and complete it in the compilation stage.
Macro definition is completed in the precompile stage. You should bracket everything that can be bracketed.

7. Left and right values

Lvalue: represents a variable that occupies a certain position in memory.
Right value: non left value.

8. Polymorphism of C + +

In a word, modify the member function with virtual in the base class, and rewrite the function in the subclass. A virtual function must be a member function of a class.
All classes with virtual functions have a one-dimensional virtual table. The object of the class has a pointer VPTR pointing to the beginning of the virtual table. The virtual table corresponds to the class, and the virtual pointer corresponds to the object.
The virtual function table does not really exist, it is only a part of the virtual table.
The structure of virtual table is as follows
offset
RTTI
Virtual function 1
Virtual function 2
Polymorphism is a class with multiple implementations of an interface, and abstract classes contain at least one virtual function( Abstract classes are classes that declare pure virtual functions.

9. Why do destructors in classes with virtual functions need to be defined as virtual functions?

The destructor of the derived class will only destruct its own part. If the fictitious function of the base class is not a virtual function, the destructor of the base class cannot be called to destruct the part of the members inherited from the base class. Therefore, there will be a general phenomenon of deletion, resulting in memory leakage.

10. Why don’t constructors be defined as virtual functions?

During the construction of a base class, the call to a virtual function is never passed to a derived class. The destructor should not throw an exception, otherwise it may cause the program to exit abnormally.

11. Friend class and friend function

In C + +, the member data of a class is usually private. When we need a function that can access private members at any time and does not belong to the class, we define this function as a friend function. A friend cannot inherit and is a one-way friend.

12. Implementation of STL container bottom layer

Vector: dynamic array
Two end queue: two way list
Double ended list: double ended array
Red black tree
Red black tree
stack:list
priority_ Queue: vector implementation
hash_ Set: hash table

13. Red black tree, B tree, B + tree

(1) Red black tree is a binary search tree and a balance tree. Red black tree is a kind of binary search tree with red black nodes and self balance. Red black tree is not a perfect balanced binary tree, but a black balanced binary tree.
(1) Each node is either black or red
(2) . the root node is black
(3) Every leaf is black
(4) The path from any root node to any leaf contains the same number of black nodes.
There are three balance strategies: left, right and color changing.
Red black trees always insert red nodes.
(2) B-tree is a kind of tree data structure, which is designed for hard disk or other storage devices. The height of a B-tree with n nodes is LGN. Insertion and deletion can be realized in O (LGN).
The characteristics of an m-order B-tree are as follows:
(1) Each node has a maximum of M children
(2) Except the root node and leaf node, each node has at least ceil (M / 2) children
(3) Each non terminal node does not contain records
The leaves of B + tree contain all keyword information, and each non terminal node is an index, VSAM

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]