My understanding of pointers in C++, save time and space (read: memory)

I’ve read a few articles about this topic but I found the following three to be the one that explained it best to me.

http://www.cplusplus.com/forum/beginner/337/
This is the c++forum where someone asks what is the purpose of pointers. The first answer gives you a clue in that pointers are not useful for ordinary variables, but when data structures get large and complex they are very useful. The answer also points to the next two links as being good resources.

https://www.cprogramming.com/tutorial/lesson6.html
This article has a paragraph where it also makes references to usefulness of pointers when having huge pieces of data. For instance ff you pass a large data structure to a function, the whole thing will have to be copied, which takes time and extra memory. If you just pass the pointer you don’t copy the data, you save memory space and the whole operation is quicker.

http://web.archive.org/web/20020223191510/http://richardbowles.tripod.com:80/cpp/cpp18.htm
This last link was a little finicky to find as the original page doesn’t exist anymore. Luckily (sometimes) nothing disappears from the internet.
This article also mentions that you save time by using pointers and that it’s used on complex data structures, so again time and memory savings.
It also goes a little deeper into declaring and assigning pointers. You can really mess things up if you don’t know what you doing, potentially crashing a system completely.

TL;DR:
You use pointers when you have large and/or complex data structures. Using pointers will save you time and memory when manipulating these structures. But it does come at a cost that you need to know what you are doing, or you will potentially ruin someone’s day.

Privacy & Terms