I need to review the C++ language. I’m going to review the questions in other people’s book. I’ll just write them down, one is to deepen my impression, and the other is for everyone’s reference.
1. Pointers and references
- 1. The memory
Pointers occupy memory space; A reference is just an alias and does not take up memory;
- 2. The initialization
Pointers may not be initialized; The reference must be initialized. If it is not initialized, an error will be reported.
- 3. Point to
The point of a pointer can change, pointing to different objects; The reference point can not be changed, from time to time;
- 4. + + operations
The ++ operation on a pointer increases the address (increasing the number of bytes pointing to the type); A ++ reference is a ++ operation to a variable;
- 5.sizeof()
The sizeof() of the pointer is the sizeof the pointer variable (an address); The sizeof() referenced is the sizeof the pointing variable;
- 6. The hierarchy
Pointers have multilevel Pointers; References are only at level 1
2. The difference between heap and stack
- 1. Management mode
The heap is programmer managed (new/delete,malloc/free); The stack is compiler managed;
- 2. Space size
The heap is a discontinuous segment of memory space (linked list) whose size is affected by the available virtual memory and the space is large. Stack is a continuous space size, the size is generally fixed (1/2m), the space is small;
- 3. Distribution method
The heap is dynamically allocated and needs to be released manually. The stack has two types of static allocation and dynamic allocation. Static allocation (such as temporary variables, function parameters, etc.) is completed by the compiler, while dynamic allocation (such as Alloca) is automatically released by the compiler.
- 4. Distributive efficiency
Heap allocation is realized by library functions, which has a complicated mechanism and low efficiency. Stack allocation provides support at the bottom of the computer, allocating special registers to store the stack address, high efficiency;
- 5. Growth direction
The heap goes up, the addresses get bigger and bigger; As the stack goes down, the addresses get smaller and smaller;
- 6. Memory fragments
Frequent memory requisition and release of heap will cause a large amount of memory fragmentation; The stack is first in and last out without causing memory fragmentation.
3. How are new and DELETE implemented? What are the similarities and differences between new and malloc
- 1. The new implementation
string* p=new string("hello");
Copy the code
The implementation of the new
//1. Allocate memory
void* mem=operate new( sizeof(string));/ / 2. The transformation
p=static_cast<string*>(mem);
//3
p->string::string("hello");
Copy the code
- 2. Delete the implementation
delete p;
Copy the code
Delete the implementation of the
//1. Destructor
p->string::~string(a);//2. Release memory
operate delete(p);
Copy the code
- 3. Similarities and differences between New and Malloc
- 1. The attribute
New /delete are C++ keywords that require compiler support. Malloc /free are library functions that require c header support. – 2. Parameter When using the new operator to allocate memory, you do not need to specify the size of the memory block. The compiler calculates the size based on the type information. Malloc, on the other hand, needs to explicitly indicate the size of memory required. – 3. Return type new Operator When the memory is allocated successfully, a pointer to the object type is returned. The type strictly matches the object and no type conversion is required. Malloc returns void*, which requires a cast to convert the void* pointer to the desired type. – 4. Allocation failure New When memory allocation fails, the BAC_alloc exception is raised. Returns NULL if malloc fails to allocate memory. – 5. User-defined type new first invokes operator new to apply for sufficient memory (usually implemented using malloc at the bottom). It then calls the constructor of the type, initializes the member variable, and finally returns a pointer to the custom type. Delete first calls the destructor and then the operator delete function to free memory (usually implemented using free underneath). Malloc /free is a library function that can only dynamically allocate and free memory, but cannot force it to do custom object construction and destruction. – 6. Overloading C++ allows overloading of the new/delete operators. Specifically, the layout of new does not allocate memory for the object, but specifies an address as the starting area of memory. Malloc does not allow overloading. The new operator dynamically allocates memory for objects from the free Store, while the malloc function dynamically allocates memory from the heap. Free storage is an abstract concept based on the new operator in C++. Any memory that is allocated through the new operator is called free storage. Heap is a term in the operating system. It is a special piece of memory maintained by the operating system and used for dynamic memory allocation of programs. C language uses MALloc to allocate memory from the heap and free to release allocated corresponding memory. Free storage is not equal to the heap, and as mentioned above, layout new can be out of the heap.
4. Differences between C and C++
5. The connection and difference between C++ and Java, including language features, garbage collection, application scenarios, etc. (Java garbage collection mechanism)
Struct and class
7. Define vs. const (compile phase, security, memory footprint, etc.)
8. Use of const and static in C++ (definition, use)
9. Considerations for using const and static in classes (defining, initializing, and using them)
10. Const class member functions in C++
11. Calculate the size of the following classes:
class A {}; //sizeof(A) = 1;
class A { virtual Fun(a){}};//sizeof(A) = 4(32-bit machine)/8(64-bit machine);
class A { static int a; }; //sizeof(A) = 1;
class A { int a; }; //sizeof(A) = 4;
class A { static int a; int b; }; //sizeof(A) = 4;
Copy the code
12. Give a code and evaluate the output
class A
{
public:
A(int x){}
}
Copy the code
A A = 1; Is it correct, and if so, what functions does it call? More often than not, these problems have different implementations in base and subclasses. (Virtual function correlation, there are many, not to say more.)
13. Introduction to STL of C++ (this series is also very important, I suggest some books and videos on this topic), including memory management allocator, functions, implementation mechanism, multithreading implementation, etc
STL source code hash table implementation
15. Difference between unordered_map and map in STL
16. Implementation of vector in STL
17. Attention to the use of vector and its reasons, the impact of frequent calls to push_back() on vector performance and why.
18. The difference between overloading and overwriting in C++
19.C ++ Memory Management (Hot issue)
20. Introduce the three main features of object orientation, with examples for each.
21. Implementation of polymorphism (to be answered with the next question)
22.C++ virtual function correlation (virtual function table, virtual function pointer), virtual function implementation principle (hot, important)
23. How should the implementation compiler handle virtual function tables
24. Why destructors are generally written as virtual functions
25. Why aren’t constructors generally defined as virtual functions
26. What happens when a virtual function is called from a constructor or destructor
27. Pure virtual functions
28. Introduction to static and dynamic binding
29. Can references be dynamically bound, and why
30. Differences between deep copy and shallow copy (examples of deep copy security)
31. Understanding of object reuse and zero copy
Introduce all C++ constructors
33. When will the copy constructor be called (three cases)
34. How to align the structure and why to align the memory?
35. Definition of memory leaks, how to detect and avoid them?
36. Handwriting implementation of intelligent pointer class (34-37 I have not met)
37. Method of debugging programs
38. How to debug coredump
39. Understanding of memory checking tools
40. Usage and application scenarios of templates
41. The concept of member initializing lists, why initializing lists with members is faster (performance advantage)?
42. Have you used C11? Do you know the new features of C11? (Some interviewers suggest familiarizing yourself with C11)
43.C++ call convention (simpler C++ function call stacking process)
44. Four C++ casts
The resources
Watchman 1208
2. Discuss the usage and difference between new/delete and malloc/free