Revisiting smart pointers
Throughout the book, we have developed a reasonable understanding of how to use raw or native C++ pointers, including the associated memory allocation and deallocation for heap instances. We have persevered through native C++ pointers because they are pervasive in existing C++ code. Having knowledge of how to properly utilize native pointers is essential in working with the volume of existing C++ code currently in use. But, for newly created code, there is a safer way to manipulate heap memory.
We have seen that dynamic memory management with native pointers is a lot of work! Especially when there may be multiple pointers to the same chunk of memory. We’ve talked about reference counting to shared resources (such as heap memory) and mechanisms for deleting memory when all instances are done with the shared memory. We also know that memory deallocation can easily be overlooked, leading to memory leakage.
We have also seen, firsthand, that errors...