Getting to know smart pointers and optionals in C++
In Chapter 4, we revisited the C++ fundamentals in order to be on the same page when it comes to the language. One instrument that is also considered a must is smart pointers. Through these, we are able to improve the safety of the program and also manage our resources more effectively. And as discussed in the earlier chapters, this is one of our main goals as system programmers. Remember the RAII principle? Smart pointers are based on this, helping the C++ developer reduce and even eliminate memory leaks. They could also help with shared memory management as you will see later in the chapter.
Memory leaks appear when we allocate memory but fail to free it. This could happen not only because we forgot to call the object’s destructor, but also when we lose the pointer to that memory address. In addition to these, there are also the wild and dangling pointers to consider as well. The first one happens when the pointer is there...