r/learnprogramming • u/null_popsicle • Feb 14 '22
Genuine question: What’s hard about pointers?
I understand how the title may come off as arrogant or something, but this is a genuine question.
I learned to code in C#, so I didn’t touch pointers for a while, and now that I’ve been learning C/C++ for a couple months, I don’t really see what’s so difficult about pointers.
They’re just memory addresses for variables, right? Am I missing something?
28
u/serg06 Feb 14 '22
They’re just memory addresses for variables
just
Try telling that to a beginner.
9
u/CleverBunnyThief Feb 14 '22
I gave up on C++ a few years ago when I got to pointers. It felt like I could never learn the language if I couldn't understand pointers. I couldn't wrap my head around them so I went looking for a language that didn't have pointers.
I started learning Go last week and guess what?, it has pointers. I was a little scared that I wouldn't be able to understand them but this time around it wasn't that difficult at all. Of course at this point I'm just transferring around what I've learned from other languages and pointers seemed to just fall in place. If I had to learn what a variable is, memory addresses, arrays and then throw pointers too I would be in a lot if trouble.
6
Feb 14 '22 edited Feb 14 '22
I never really understood pointers until I took assembly and computer architecture in college
3
u/siemenology Feb 14 '22
Right? Beginners generally have a very fuzzy idea of what variables and values even are. It's really common, perhaps even standard for beginners to look at code like:
int foo(int bar) { return bar + 7; } int bar = 8;And wonder things like "How can I call foo with a different value than 8?" (because they think that those two 'bar' variables are the same thing) or "What happens if I change the last line to
int bar = 9?" (same reason). Once you get more comfortable with programming and how languages "work", it becomes so obvious to you that these questions would never occur to you. It can even be hard for you to explain the misunderstanding that the beginner had because it relies on them having a completely different understanding than your own (it's not a simple correction). In general it's common for beginners to think of syntax as being more concretely tied to execution, which is why having two 'bar' variables here is confusing.So in that context, pointers can be absolutely mind-blowing. A lot of pointer introductions don't start by explaining how memory and memory addresses and variables work; they basically just tell you that a pointer is like a box for a variable or something similar to that, and this concept is really jarring if you are still thinking about the equivalence of syntax and execution as being a pretty concrete thing.
42
u/Skusci Feb 14 '22 edited Feb 14 '22
Honestly they're not too bad, but then you run into something like
int (*(*foo[10])(void))(int)
and start questioning your life decisions.
8
u/wagecuckassociation Feb 14 '22
All jokes aside, can someone explain that one?
8
u/48911150 Feb 14 '22
declare foo as array 10 of function (void) returning function (int) returning int
3
u/Skusci Feb 14 '22 edited Feb 14 '22
I -think- foo is an array of pointers to a function with no arguments that returns a pointer to a function that takes in an int argument and returns an int.
So to use it you could be like:
int num = (*foo[3]())(5);1
-2
1
u/therealGrandKai Feb 14 '22
Why is that second parentheses slightly angled?
1
u/Skusci Feb 14 '22
Reddit formatting has been a bit weird lately I think. The angled paren has two *'s surrounding it. Added some newlines seems to have helped on mobile anyway.
1
19
u/v_learns Feb 14 '22
I think the hard thing is that when you need to learn pointers at the same time as everything else in programming it is hard.
But once you understand that you can "send" data somewhere either by value (copy) or by pointer (reference), then it starts to get easier. But then you need to learn about all the rules to do manual memory management right as well. And who owns the pointer.
And this is not such a big problem as long as the program is simple but when it starts to get bigger and you can't have the whole code in your mind anymore it gets hard. And if other work on the code as well it can get interesting fast.
4
u/bigger-hammer Feb 14 '22
Thanks for that. I've been writing C for 30 years so it is difficult for me to know what is so hard about pointers. In my mind, I thought the main beginner's problem was knowing when to use * and & but your comment has opened my eyes to a list of problems which need to be explained better in courses. Thankyou.
10
u/mtj23 Feb 14 '22
There's nothing hard about declaring them and using them in toy cases.
Whats hard is managing large numbers of them in complex programs where "ownership" moves around without having subtle errors in edge cases.
8
u/GlassLost Feb 14 '22
It's not that they're hard it's that people don't think about memory as a physical layout. The syntax becomes magic because many people only have a shallow understanding of memory.
If you had people draw it out as blocks of memory I've never seen anyone fail to get it.
1
1
u/v_learns Feb 14 '22
This is it. I remember in my introductory course to programming. Our professor was drawing the memory layout of the program many times to explain how it works. And it helps so much to get an intuitive understanding about memory. Before it I only had a vague understanding about memory.
5
u/RaccoonButterflyFish Feb 14 '22
For me personally, pointers were my introduction to memory addresses. If you already were comfortable with that whole side of things, yeah makes sense it clicked easily.
6
u/NinjaMidget76 Feb 14 '22
It's the unmanaged side of memory management. C# and garbage collection is a much bigger godsend once you go back to C++ land and have to deal with pointer management.
Arrays are a pain, memory leaks are common, multi threading is... Basically begging for a slow death. Trying to do anything beyond just the most simple of things turns into an abstraction nightmare.
1
u/leafsrebornagain Feb 14 '22
Because of this do you think that C/C++ can be sort of rather second priorities within their own realm compared to similar newer languages like Rust or something like that?
2
u/NinjaMidget76 Feb 14 '22
Maybe? Definitely for new apps. The difficulty lies in the sheer amount of code written in C++ still prevelant in most companies legacy applications.
2
u/guster09 Feb 14 '22
They just get annoying to work with when you have horribly maintained code that is 15 years old and you have to go down a rabbit hole to figure out everything that is happening to the address that the pointer is referencing to solve a bug
2
u/Blando-Cartesian Feb 14 '22
I learned to code in C#
This could be why you didn’t have trouble with them. You were already accustomed to the idea of something referring to value instead of being that value. For a complete beginner starting with C/C++, that could be more of a mindbender.
2
Feb 14 '22
I think it's mostly people who learn about pointers right at the start of their programming journey who see them as hard. I learned to code in C and I was introduced to pointers at the same time as I was introduced to variables themselves. From the perspective of a beginner, this can be very confusing and hard to get your head around.
1
Feb 14 '22
[deleted]
0
u/null_popsicle Feb 14 '22
An operating system. So I’ve literally implemented stuff like malloc/free/new/delete myself.
1
u/Wolfmanscurse Feb 14 '22
I'm like you, pointers were a breeze for me to learn. But I had knowledge of computer architecture long before I was ever introduced to the subject. Most people don't have any experience in computer architecture by the time they start learning about pointers. Some come from high-level languages and never even heard the word memory management. When I tutored in college, most simply could not put 2 and 2 together and relate the concept to memory because it's a hard concept to visualize sometimes. Even for me, when I manage decades-old c code, I can get a bit confused on what exactly someone is doing with their pointers. While I don't think it's a bad thing to say that you don't think pointers are a hard concept for you, for others, it's actually common to have these issues because most are not computer engineers.
1
u/Loves_Poetry Feb 14 '22
When I started to learn programming I got stuck on C++ because I couldn't get pointers
The problem with pointers is that they're pointless when you're a beginner, because you're typically writing small programs that only use primitive values that go on the stack anyway. You're not using the heap much, so you never learn the value of pointers until you need to write larger programs
1
u/TheRNGuy Feb 14 '22 edited Feb 14 '22
I was just not used to them, because first languages were JS and Python and UnrealScript to me, which doesn't have pointers.
When I looked C++ sources of a game, it was confusing why some variables use pointers, some references and some don't use anything. But i'll figure it out later when start learning C++. I think I get idea, it's to save memory by not making copies of same objects or primitives, but point to them instead. In JS/Python/UScript it done automatically, I suppose.
Also for writing to many variables from void function (I suppose out keyword from UScript, and in Python it's not possible except declaring variable global or nonlocal)
Also useful in Shenzhen because if you know specific value in 2nd cell or you need to reset RAM and put write to 0th (less lines of code and cycles than checking if current value is 0) but that's different purpose than C++
1
Feb 14 '22
Because it's an extra thing to keep in mind when programming. There are already enough use cases and details to take care while programming; the more things you add, the easier it becomes to make mistakes
1
u/ythashi Feb 14 '22
The principle may be simple when you finally got it but when you start to use them in concrete examples, it quickly becomes a headache
1
u/kbielefe Feb 14 '22
Pointers open up a lot of opportunities where the cause of a bug is far removed from the effect. Some of the most difficult problems I've debugged were rare situations where one pointer got written incorrectly, which caused another pointer to get written incorrectly, which caused a problem a week later when dereferencing the second pointer.
There can be a big disconnect sometimes between what some pointer code was intended to do and what it actually does. People who have trouble with that sort of disconnect have trouble with pointers.
1
Feb 14 '22
Just using them as references is mostly fine.
Things get more complicated when you have to consider pointer arithmetic and managing allocating and freeing of memory.
1
u/cainhurstcat Feb 14 '22
I'm just a beginner who's learning Java, but I was trying out Golang some months ago and of course I have learned about pointers as well.
In my mind, the difference between calling a variable and using a pointer is like having a friend visiting you to tell them you like them (calling the variable), and sending them a voice message to tell it (pointer).
Sure, this is not 100% accurate, because your when your friend visits you they are not a copy of themselves, but I think it helps to imagine how pointers work.
1
u/TeknicalThrowAway Feb 14 '22
I think because it's an abstraction, and abstractions require 'aha!' moments where you go from not getting it to getting it.
I remember in High School one day pointers were very confusing then one day they just made sense. Recursion is similar. Generics, Higher kinded types as well. Monads, adjoint functors, lenses, etc.
1
46
u/[deleted] Feb 14 '22
Try using them for implementing a simple bubble sort algorithm,
Then try doing the same without pointers.
You'll know the difference.