1. What are the problems with traditional Pointers in C++?
- (1) Manually manage memory resources
- Prone to memory leakage (forget to release, exceptions, etc.)
- (3) Wild pointer is generated after release
2. What Pointers were introduced to solve the pain points of traditional Pointers? How about a pointer only format? Do smart Pointers point to heap objects or stack objects?
- Smart Pointer introduced: auto_ptr, shared_ptr, unique_ptr
- Smart pointer pointing
The heap object
- If you use a pointer a pointer points to
The stack object
Because the stack object itself will be released when it is applied, the smart pointer will cause it to be released again, and there will bedouble free
The problem
int main(a) {
try {
// The smart pointer p points to the person object in the heap.
shared_ptr<Person> p(new Person());
p->run();
throw "Abnormal";
} catch(...). {cout << "Abnormal" << endl;
}
// Even if an exception is thrown, '~Person()' is still released
return 0;
}
Copy the code
3. How to implement a simple custom smart pointer?
template <typename T>
class SmartPtr {
T *m_obj;
public:
SmartPtr(T *data): m_obj(data) {}
~SmartPtr() {
if (m_obj == nullptr) return;
delete m_obj;
}
// Override the operator -> to call the m_obj method properly
T * operator- > () {returnm_obj; }};using namespace std;
int main(a) {
cout << 1 << endl;
{
SmartPtr<Person> sp = SmartPtr<Person>(new Person());
sp->run();
}
cout << 3 << endl;
return 0;
}
Copy the code
4. What are the advantages of shared_ptr compared with auto_ptr?
- Multiple shared_ptr can point to the same object, and the object is automatically released when the last shared_PTR ends in scope
- You can initialize a new smart pointer from an existing smart pointer
- Shared_ptr is exactly the same as the normal pointer in OC
5. Explain the principle of Shared_PTR
- a
shared_ptr
Will be generated on an objectStrong reference
- Each object has a corresponding one
Strong reference counting
, which records the number of shared_ptr strong references to the current object. The strong reference count can be obtained by use_count in shared_ptr - When a new shared_ptr points to an object, the object’s strong reference count is +1
- When there is a shareD_ptr destruction (such as the end of scope), the object’s strong reference count is -1
- When an object has a strong reference count of zero (when there is no share_pTR pointing to the object), the object is automatically destroyed (destructor call)
int main(a) {
shared_ptr<Person> p1;
{
shared_ptr<Person> p2 = shared_ptr<Person>(new Person());
cout << p2.use_count() << endl; / / 1
p1 = p2;
cout << p1.use_count() << endl; / / 2
shared_ptr<Person> p3 = p2;
cout << p1.use_count() << endl; / / 3
}
cout << p1.use_count() << endl; / / 1
return 0;
}
Copy the code
6. Will there be a problem with the following code?
int main(a) {
Person *p = new Person();
{
shared_ptr<Person> p1(p);
}
{
shared_ptr<Person> p2(p);
}
return 0;
}
Copy the code
- You’re going to have multiple releases of P, you’re going to crash
7. How do smart Pointers have circular references? How to solve it?
weak_ptr
Will be generated on an objectA weak reference
weak_ptr
Can solve the shared_ptr circular reference problem
8. Unique_ptr (Useless)
- Ensure that only one smart pointer points to the object