4 ms·
> If pointers are so "simple", why are they so confusing?!? Because pointers are one of those concepts that's easier to use than to explain. I disagree (the re
by melvinroest 6y ago
> If pointers are so "simple", why are they so confusing?!? Because pointers are one of those concepts that's easier to use than to explain.
I disagree (the rest of your comment I fully agree with, FYI). What I personally found difficult is to understand that: when you dereference an invalid memory address, then the dereferencing operation is working, but the OS/runtime environment won't allow you to do it!
Wait, what? Why?! Who cares my value is wonky, why is my program giving an error? Just obtain the wonky value! (is what I thought when I first did it)
The reason it's so difficult because at this stage you're still using printf for debugging, while you really should use a debugger, to really understand what's going on under the hood. Preferably, one also learns a modicum of x86 to really see it immediately.
The concept of dereferencing the wrong values isn't explained a lot. How to troubleshoot segfaults isn't explained at all in beginner tutorials, not even in my own (I just wrote an explanation for fun as a comment in the same thread [1]).
Also FWIW, when I heard the concept of pointers I found them intuitive immediately. I tend to think and reflect a lot, and the explanation of referencing and dereferencing almost sound like some proverbial life wisdom to me.
I found their use a lot harder, in part because I found code syntax hard. In another part, because I used to find "doing things" and "building things" (of any kind) an insurmountable task.
---
With that said, I do agree with the rest of your explanation. I suppose it also depends on who teaches pointers to you. I got a classroom explanation first, a few examples and then some exercises. Then suddenly, I had to write a 500 line C program. Then all the complexity storms at you like a rabid zombie army.
But if you're used to writing small C programs all the time and picked up on pointers that way, but then suddenly have to explain it. Then, I can see how that's hard.
[1] https://news.ycombinator.com/item?id=23550040 https://news.ycombinator.com/item?id=23550040
- scoutt 6y agoI think the real difference comes with the knowledge the person is carrying when learning C. For example, people that are familiar with concepts like classes, string objects, arrays, etc. brought from higher level languages are the ones that I see have much more problems when understanding memory management (and thus pointers) in C. For example, for a higher-level developer, a string might mean some kind of object that knows its length, auto-expands, etc. When in C a string is just an array that it will be overflown at the very first chance. Same with other objects. I find that a person that doesn't know much about languages are the ones that better understand the basic concepts of C (like pointers).
- melvinroest 6y ago> For example, for a higher-level developer, a string might mean some kind of object that knows its length, auto-expands, etc. When in C a string is just an array that it will be overflown at the very first chance. Same with other objects. Funny, my programming knowledge in Java helped me because of the whole concept of what a reference is and a literal value. In my first Java programming class, 8 years ago, I learned that you need to have references, otherwise things will take up too much memory. My teacher took a humongous object as an example and asked "what if I need to access this at 10000 different places, but I really only need one physical copy of it?" I was pretty upset that you had to do all of this bookkeeping and accounting. I remember being pretty vocal about that it just didn't make sense to my why one couldn't just copy stuff. I guess we all grow up at some point :) When I saw pointers for the first time (to be fair, years later), I was thinking "ah so a pointer is basically a reference". Other people would say "a pointer is a whole lot more than just a reference, you can do pointer arithmetic with (and so on)". And I was just thinking to myself: alright, it's a reference on steroids, but the groundbreaking essential idea of the pointer is that it refers to something, just like a reference in Java. And then I'd hit a segmentation fault and doubt my own understanding. Learning programming felt emotionally turbulent to say the least, haha.
- patrick5415 6y agoMy experience was nearly the opposite. I tried to teach myself c 15 years ago and found pointers completely bewildering. I didn’t really know how to program any other language then. After a 10 year, non-technical interlude, I went to grad school and spent 5 years writing matlab. In the last year, I decided to learn c again. This time, pointers seemed completely straightforward. The most difficult part for me this round was keeping the syntax straight.
- melvinroest 6y agoOut of curiosity, what was your issue the first time?
- com2kid 6y ago> Wait, what? Why?! Who cares my value is wonky, why is my program giving an error? Just obtain the wonky value! (is what I thought when I first did it) Program on an MCU sometime, no memory protection. Feel free to tap dance all around RAM. It is rather liberating! Really though, pointers are IMHO easy to understand, they are the address of an actual physical location in the real world. If you have a good enough microscope, or a large enough RAM chip, it would be possible to find the literal physical location that a pointer is referencing. Pointers are the ultimate un abstraction. They represent something real!
- Gibbon1 6y agoThis here exactly. Dereferencing null. Oh maybe the compiler can figure out a pointer is null and try and ratfuck you in the name of some obscure 'optimization'. But when it can't what happens depends on the machine. Big iron machines with mmu's will invariably throw a seg fault. Some MCU's without an MMU will either seg fault because no physical memory exists[1] or return garbage. Some machines you get interesting things like the initial stack pointer. Or a memory mapped register. [1] Trapping seg faults and logging address of the offender takes most of the pain away.
- com2kid 6y agoThis is why MCUs are better. 0 is where memory starts, there is perfectly valid data at address 0. Pointers aren't the problem, NULL coloring pointers and the compiler tracking that, that is the problem. In a sane world '0' would be a perfectly valid address just like any other.