In this section, the reader will learn how to use smart pointers to increase the safety, reliability, and stability of their program, while also adhering to the C++ Core Guidelines.
Understanding smart pointers and ownership
The std::unique_ptr{} pointer
It should be clear by now that C++ provides an extensive set of APIs for allocating and deallocating dynamic memory. It should also be clear that whether you are using malloc()/free() or new()/delete(), errors are not only possible but likely in large applications. For example, you might forget to release memory back to the heap:
#include <iostream>
int main()
{
auto ptr = new int;
std::cout << ptr << '\n';
}
// > g++ -std=c++17 scratchpad...