[C++] Interview questions

1. It is said that C++ is an object-oriented language. Can you [expand] introduce the three characteristics of object-oriented?

Encapsulation: Encapsulation is a centralized management idea that combines internal data and implementation methods without exposing the internal data and implementation methods to the outside world. It only provides a few interfaces to the outside world to complete function calls and data operations. The security and consistency of data are guaranteed.

Inheritance: Inheritance means that one class can inherit the methods and data of another class, which can improve the reusability of code and establish the relationship between classes.

Polymorphism: Polymorphism means that the same method has different behaviors for different objects, which improves the flexibility of the code.

2. Have you ever understood the underlying implementation of polymorphism?

The bottom layer of polymorphism is implemented through a virtual function pointer. The virtual function pointer points to a virtual function table. The address of each function is stored in the virtual function table. The virtual function table is an array of pointers, and the virtual function table stores function pointers. , when an expression satisfies polymorphism, it determines the type not in the compilation phase, but in the running phase to determine what type it is, and then calls the virtual function table according to different objects.

3. How is the virtual function implemented at the bottom level?

When a function in a class is virtualmodified, a virtual function pointer will appear, and the virtual function pointer points to the virtual function table.

4. (Scenario 1) There are two classes whose instance variables and function methods they support are exactly the same. One class implements a virtual function. What is the difference between them? Do they take up the same amount of memory when generating an instance object?

The memory occupied is different. If the virtual function is implemented, there will be one more virtual function pointer, which will take up 4/8 bytes of space, and the memory occupied will be different.

5. (Scenario 2) There are four classes B and C inheriting from A, and D inheriting from B and C (multiple inheritance). There is a public function in A, and then each of B and C has rewritten it, and then wants to call it from D. How to call the implementation of B or C?

Directly use the class domain operator ::to specify the class domain call.

6. It’s still the above scenario. There is a public variable in A. B and C inherit from A, and D inherits from B and C. Then there are several public variables stored in D (it has been guiding me, one, Two portions, three portions)

This question needs to be discussed in categories.

Ordinary inheritance: two shares (one share each for B and C).

Virtual inheritance: one copy, placed directly in the public area.

7. What is the difference between malloc and new?

1. The return type of malloc is (void*) and requires manual cast.

2. malloc returns a memory allocation error NULL, while new throws an exception.

3. When new allocates memory, it will call the constructor for initialization, and malloc needs to be initialized manually.

4. When malloc allocates memory, you need to manually calculate how much space to open, but new does not.

5. New uses delete to release memory, and malloc uses free.

6. The bottom layer of new is also called operator new, and operator newmalloc is also called to realize memory allocation.

8. In addition to allocating memory, does new have other additional operations than malloc?

The constructor will be called for initialization.

9. New actually does two things, one is to allocate memory, and the other is to call the constructor of the instance. Have you ever understood that new can only perform one operation? For example, only allocate memory without calling the constructor, or only call the constructor without allocating memory?

No memory is allocated, only the constructor is called: position new( placement new) as well operator new.

The constructor is not called, only memory is allocated: new (std::nothrow) type.

10. Calculate the sizeof of the following two structures.

struct {
    
    
    char A;
    char B;
    int C;
}

struct {
    
    
    char A;
    int C;
    char B;
}

Memory alignment issues.

The first one is 8 bytes.

The second one is 12 bytes.

11. I see that you wrote that you are familiar with STL. Have you learned about smart pointers?

1. The earliest smart pointer was auto_ptr, but this smart pointer did not fully realize the functions of the pointer. It mainly realized RAIIthe idea of ​​​​and permission transfer.

2. Then it appeared unique_ptrthat the approach of this smart pointer was rather crude, not allowing copying and prohibiting calling copy constructors.

3. Then shared_ptr, this smart pointer solves the original problem: an address can only be pointed to by one smart pointer, otherwise the same block of memory will be released twice. Solution: Use reference counting. But there is still the problem of circular references.

4. weak_ptr, an auxiliary smart pointer, used to solve the problem of circular reference, allowing the internal pointer variable weak_ptrto be used to represent it, weak_ptrwithout modifying the reference count, so it can solve the problem of circular reference very well.

12. Give an example of a smart pointer in a practical scenario? Why use it? How to use it? Is it okay if I don’t use it?

When it comes to exception safety, it's best to use smart pointers.

For example, the following code:

#include<iostream>
using namespace std;

int div()
{
     
     
	int a, b;
	cin >> a >> b;
	if (b == 0)
		throw invalid_argument("除0错误");
	return a / b;
}
void Func()
{
     
     
	// 1、如果p1这里new 抛异常会如何?
	// 2、如果p2这里new 抛异常会如何?
	// 3、如果div调用这里又会抛异常会如何?
	int* p1 = new int;
	int* p2 = new int;
	cout << div() << endl;
	delete p1;
	delete p2;
}
int main()
{
     
     
	try
	{
     
     
		Func();
	}
	catch (exception& e)
	{
     
     
		cout << e.what() << endl;
	}
	return 0;
}

Failure to use smart pointers will lead to problems such as various memories not being released, or no space being opened.

Can pointers be used without pointers? Yes, but it requires several layers of exception catching, which is too troublesome and the readability of the code becomes very low.

13. Just now you mentioned that the bottom layer of shared_ptr is shared using a reference count. Do you know about other memory management methods?

The automatic memory recycling mechanism in JAVA uses a relatively complex set of algorithms to calculate the timing of recycling, and there is no in-depth understanding of the details.

14. Have you ever understood the expansion mechanism of vector containers commonly used in STL?

The expansion mechanism is different on different platforms.

VS: 1.5 times.

Linux: 2x.

Expansion is to reopen a space, copy the original content, and then destroy the original space.

15: Is it okay to expand the capacity three times?

Yes, but it may result in a lot of wasted space.

16: Do you know the initial capacity of vector? When will the initial expansion take place?

The capacity will be expanded when an element is added for the first time, and the first expansion will generally be set to 8 or 16.

Guess you like

Origin blog.csdn.net/AkieMo/article/details/132278215