12th Chapter Dynamic Memory
1. Apply for and use shared_ptr. P400
2. shared_ptr count. P402
assigning, copying, passing a smart pointer to a function, or returning a smart pointer to a function will increase the current count of smart pointers.
3. Examples of sharing data between different objects using smart pointers.
4. Use new to dynamically request memory. P407
By default, the memory objects that are requested by new are initialized by default.
5. The const object for the new application must be initialized. P408
If you request a built-in type, you must initialize it with parentheses (). If the class type is requested, it can be initialized by default if the class object has a default constructor. However, if the class object does not have a default constructor , It must be initialized with a different constructor .
6. Use Delete. P409
Delete You can only delete the pointer and the pointer must have been used New the requested memory space .
7. shared_ptr and new are used together. P412
8. Do not mix smart pointers and built-in pointers. P 413
9. Unique_ptr object initialization. P417
Because Unique_ptr "owns" its object, it cannot copy and assign values, and it can only be initialized with a built-in pointer through parentheses ().
But we can copy or assign a unique_ptr that is about to be consumed. As shown in the following function:
The reset () function and the release () function usage of the Unique_ptr object. P418
One by one. Unique_ptr pass its own undelete: P419
shared_ptr passing its own undelete is also similar to unique_ptr.
Weak_ptr Use Example P420.
The dynamic array is assigned to new. P424
Either mode 1 or Mode 2, you get a pointer to an array element type instead of an array type , so if you use begin or end for it is wrong
14. Use smart pointers to manage dynamic arrays. P425
It is recommended to use UNIQUE_PTR to manage arrays.
C + + Primer Learning Summary 12th Chapter dynamic Memory