So basically pointers are just a way to be able to work with very large amounts of data without overwhelming the stack, and to be able to access the full data in memory, right? |
while I think you nailed the modern common use cases pretty well, pointers can do a lot of cool stuff. Here are a few of them, most of which you may never have to do in modern c++ because it does much of it for you, but not ALL of it :)
- pointers can chain data. A linked list, which is provided by the STL, could use pointers to link the head to next to next ... to last. The STL does NOT provide a tree or graph structure and you would need to use a third party library or roll your own there, so this COULD be something you do one day.
- pointers can be very like a 'reference' to data. Not just objects, but like just an integer, same as int &x. You see less of this in modern programming but from time to time data is passed to a function in a pointer or as a pointer, and it is free to be modified (the original!) by the function. This is a huge concept... the pointer can't be modified (its passed by value) but the thing pointed to CAN be. Re-read that until you have it internalized.
- pointers can have no type (a "void" pointer). You WILL use this, but may not write a lot of code that exploits the idea. Thread libraries are one of the first places you might see it, as most of those pass the arguments to the thread function via a void pointer.
- pointers can be used for iteration and iterative access of memory blocks. You see less of this today, thanks to range based for loops, but for example you can say p = &vectorvar[0]; for(stuff) *p = 0; ++p; and p will move to vectorvar[1], [2], ... I never liked this way of doing things, but you may see it in older code or if you get a C file in your c++. One place where that is useful is negative iteration. You can iterate a pointer backwards and even with a negative value, eg ptr[-10] = 42; That is normally not very attractive or useful, but for like a counting sort based algorithm... say you are dealing with signed bytes. An array of 256 values, take a pointer into the middle of it, and you can use the pointer with a signed value to count up how many times -42 appeared in your data. Its one of those things you may never do in a lifetime, but its there if you need it.
- pointers can be used for performance tuning. Say you had a big fat person object with all their person stuff like name, address, education, finances, all that kind of thing. Some annoying user comes along and wants your list of millions of people arranged by their income, but your data is sorted by last name then by first name then by location (eg city or something). You could run your big fat objects through std::sort and its not bad, maybe it takes 10 seconds to get it back out. But it would be done before your finger left the mouse if you sorted a vector of pointers to the data by the criteria you wanted, which is akin to sorting a list of integers instead of a list of fat objects.
There are more things like these where creative use of pointers goes way beyond just memory management and polymorphing.
I know what you meant, but pointers are not a 'way'. Pointers are a 'thing'. Mechanically, pointers are just special integers. Conceptually, they are a way to store an address in memory. Think of memory as a giant array/vector. A pointer is an integer that holds a location in that array. vector<int> memory; ... int ptr = 42; x = memory[ptr]; It works a lot like that, conceptually. So much so that most of what I told you up there can be done with an array, including building a linked list, using the index of each location instead of a pointer to chain the data.