In this section of the course,
we'll learn about the various types of smart pointers in c++ and how to use them.
First, we'll understand why we need smart pointers at all.
After all, there must be an issue with the way that raw pointers work
that motivated the need for smart pointers.
So we'll review some of the problems associated with raw pointers.
Then we'll see what smart pointers are and understand how the address the issues with raw pointers.
One of the most important concepts to understand in this section
is that of ownership and RAII or resource acquisition is initialization.
I'll go over this idiom since it's central to understanding not only smart pointers
but resource management in general.
Then we'll look at each type of c++ smart pointer and see how they work and how to use them.
Ultimately, we want to write code that has no news and no deletes.
So all the memory management is handled by c++.
If c++ can automatically clean up the heat whenever a pointer is no longer needed,
then we'll never leak memory, and we don't have to worry about needing to know when a pointer is no longer needed.
That's the idea anyway. Smart pointers aren't perfect,
but they can certainly make our code better in most cases.
First, we'll learn about the unique pointer, which is exactly what it sounds like.
A unique pointer to keep storage that is not shared and the ownership is clear.
When you know who owns the pointer, then you know who needs to delete the pointer
or at least c++ does.
Then we'll learn about shared pointers. Shared pointers are pointers that share common heap
storage and can be very useful.
Then we'll learn about weak pointers, and how they can help us avoid a problem
associated with shared pointers.
Finally, we'll see how we can use custom deleters
so that c++ can call a specific version of delete
that does exactly what we want.
Smart pointers are very important part of modern c++,
and we want to use them whenever we can to help us manage memory.