- Joined
- Feb 18, 2022
There is, in practice, only one good reason to use pointers, and that is so you don't have to allocate memory twice.I've been coding for years but I still don't understand pointers. I mean I understand what they are but I have a hard time coming for a rationale to use them. All of the newer languages within the past couple decades seem to be getting rid of them or at least transforming them into something different so maybe I'm on the right track.
Let's say I have a linked list called myList and a function called myFunc. myList being a list, takes up a whole lot of space. When I pass myList as an argument to myFunc, it's allocating a second linked list worth of memory with the same value as myVar so myFunc can work with it without messing with the original variable. One list, twice the space. A pointer would have only taken up a single word of space and accomplished the same thing in most cases.
Avoiding this is vital in old-ass systems with 64 kilobytes of RAM, but in modern day, there is zero reason to worry about micro-optimizations like that unless you're a systems programmer or are writing kernels or libraries or something else extremely low level. Odds are nearly 100% that while you were worrying about a few extra kilobytes of memory being reserved, you missed something literally thousands of times worse.
Modern programming languages either did away with or strongly discourage the use of pointers for a reason. They make code hard to read, hard to write, and offer almost no benefit to all but a tiny minority of programmers. If you really, truly need them, you should be coding in C++, if not C.
Last edited: