Learn How To Use Pointers The C++ Way And Stop Avoiding Them
Let me give you an example of pointers that you can understand. Then once you understand this, you’ll understand what this class provides and why you need it.
Okay, what do you do when you want to take a break from reading a book? Not a five second break to answer a quick question. The kind of break where you’re done reading for the day.
You probably either try to memorize the page number, or you use a bookmark, or you fold the corner of the page. I never liked folding pages myself. I like to finish a book and have it still in the same condition as when I started. I don’t like creases of any kind. But that’s just me.
Let’s say you remember the page number was 100 and come back later. You can get a fairly good idea of where page 100 is and open the book close to your spot. With just a few extra page flips, you’re back at page 100 and reading again.
For this example, I’m going to use a special kind of book. You might know of this type as “choose your own adventure” or something similar. Here’s how it works.
You start reading your new book and after reading a few pages, it tells you to go to page twenty.
Out of curiosity, you read the next page anyway and it doesn’t make sense. There’s some dialog with a new character that you have no idea who it is.
Maybe jumping to page twenty isn’t such a bad idea after all. So you go there and find the story continues just as you expect.
This pattern continues throughout the book. Sometimes you jump almost to the end of the book and sometimes you go back to almost the beginning. You may not always get a fixed page number. Instead, you might have to count pages ahead or count pages to go back.
And sometimes, you even get to choose which way to go. The story might ask you what you want the hero to do. If you want to buy supplies from a store, then turn to page ninety. And if you want to run forward with what you have to get a head start, then turn five pages back.
If you started out folding the page corner when you want to take a break instead of using a bookmark, you quickly find it’s hard to find the folded page with this book system. You have to flip through the book looking for the folded corner. Sometimes you find old creases that you have to ignore. The bookmark is better because there’s only one and it can move around. The bookmark lets you go straight to your spot. It’s just like memorizing the page number without worrying about forgetting.
Okay, let’s stop here for a moment and figure out what all this has to do with pointers. A bookmark acts like a pointer because it points directly to your current page. The folded pages are really just markers or flags which is why they become harder to find when you start moving around in the book.
It might seem like the bookmark is the best solution. And it probably is for real books where you can stick a piece of paper into the book. You can’t really stick pieces of paper into your computer though. They tend to get stuck in the fans.
What problem does the bookmark solve, really? It lets you point to the current page without worrying about forgetting the page number. What if you just wrote the page number down on another piece of paper? Then you wouldn’t forget it, right? Unless you lose the paper.
This is where computers can do something very easy that we find hard. A computer can remember a number without worrying about forgetting it. And as long as you write your code correctly, you don’t need to worry about losing it either.
While both a bookmark and remembering the page number can work, only the second solution works with code. This is called a pointer. All it really does is give you the ability to remember where something is in memory. A computer’s memory is divided into individual locations and each location has an address just like how each page in the book has a page number.
And while we can usually get close to a page number, a computer can go directly to an address in memory without any error. Not only does it remember numbers, it can follow pointers quickly and precisely.
What about the example where you had to jump ahead five pages or go back two pages? These are also pointers but a different type. They need a starting point in order to make any sense. You have to start at some page before going forward five pages can make any sense. The amount to go forward or backward is relative to the starting point.
You’ll learn about these types of pointers too. They’re called relative pointers and they don’t really point to a specific spot in memory but instead point away a certain distance from some other address.
Now that you know what pointers do, we can talk about why you’ll need them and what you’ll learn in this class.
It turns out that using a pointer to remember your current location in something is really useful in coding. This wasn’t such a made-up example after all.
But even more useful for pointers is when you need to ask for some memory. You don’t always know how much memory your application will need when you’re writing your code. So it’s better to handle this while your app is running. You use a pointer to the beginning of the memory you receive.
There’s lots of details I’m skipping over here in this explanation. There’s different types of pointers, how to declare them and initialize them, and how to use them. All of that is covered in the first part of this class.
The second part shows you how to deal with one of the biggest complaints I hear about C++. Memory management. Remember that block of memory you asked for and used a pointer to track? Well, you need to return the memory when you’re done. This is good memory management. And it’s really hard to do on your own.
This is where smart pointers come to the rescue. You’ll learn how to use smart pointers and even how they work. There’s nothing magical or even smart about them. The smart thing is to use them. Maybe they should be called smart programmer pointers instead. Because they’re the smart choice for you to make.