14

Before I decided to learn C, I had heard tell of pointers being "hard to use". Of course I thought "maybe so, " after all, that was basically the only thing I heard about pointers, "they are hard!".

And so, when I learned C, and I got to the part about pointers, I was expecting at least some trouble (I can't know everything) and it was just... easy? Maybe the trickiest thing was how * has two different meanings based on the context (declare/dereference) but that was easy too.

Why the hell is all I hear from people about pointers is that they're difficult?

Comments
  • 11
    Some people have issues navigating paper bags.

    Jokes aside. They might be referring to double and triple pointers or function pointers. Or not realizing deleting memory doesn't alter the address that is stored in the pointer. They probably got bit by something that wasn't obvious. Over time you figure those out. But maybe they are not obvious at first. It could also be issues with understanding computer architecture.

    Another issue could be using malloc, and not doing the math right to get the appropriate amount of memory allocated.
  • 4
    @Demolishun Constant pointer or constant pointer value or constant pointer to constant value. Having to carry the length of things along with a pointer to them. Better not forget to terminate that string. null pointers.
  • 2
    @ElectroArchiver You forget all the "fun" things if you are not doing them all the time.
  • 6
    Pointers is not just *. It's also the math of offsets for each datatype. Like @Demolishun said

    oh, and also function pointers.
  • 13
    You heard about pointers being hard, and you thought it referred to *learning about* pointers, when in fact the claim is probably that *using* pointers is hard, because although the concept is simple, they're massive footguns.
  • 3
    @netikras I don't know what you mean. T* is incremented by exactly sizeof(T). If you even acknowledge the exact values you're probably breaking the standard.
  • 2
    @netikras Function pointers are difficult if you try too hard to imagine pointers as numbers and not references. A function pointer can be thought of as the name of the function. In fact, that is exactly how many interpreted languages used to emulate the idea.
  • 1
    Yeah no, pointers are easy. No idea what the others are even on about. No amount of pointer nesting, pointer arithmetics or pointer management is really *hard* as long as you keep being mindful of how your memory is managed, that is how is it allocated, how is it initialized, how many references to it there are, how are those references managed and when and how is the memory deallocated.

    It's a lot to keep in mind while using pointers, but at no point I would describe it as "hard". In fact pointers aren't hard, good memory management is hard, but pointers are just the gateway drug
  • 2
    @lorentz That's true - It's certainly why many modern languages don't use them, I imagine. Better ways to manage that idea behind the scenes with the extra abstraction.
  • 1
    @Hazarth I have found quite a few weird ass bugs that didn't produce segfaults (segfaults are better) due to pointers pointing to very wrong things. Some code I wrote, some code I didn't. One bug disappeared when we changed frameworks, and came back when we updated frameworks. I finally found the issue and someone had pointed to an expired pointer. It would crash once in a while and crash always showed up in unrelated code. So they can lead to very difficult to diagnose issues. And no, valgrind or heob didn't find it.
  • 1
    @Demolishun huh, I'm surprised Valgrind didn't catch it. I find it's pretty good. Though to be honest, I didn't use Valgrind too much, I was always used to manage my own memory and I did cause my fair share of weird, borderland un-debuggable problems xD. Yeah pointers can be a headache. But I'd still argue that they get a reputation of being "hard" because some people do actually find them hard to understand. I never had the problem, but during Uni I had couple of people ask me about them and they had this really blank stare when I tried to explain why their code wasn't working and such...

    So I always just accepted that pointers are not hard, just some people really do find them hard. It's almost as if some people just can't imagine the computer memory as a physical thing. At least that's the feeling I always got from them
  • 2
    @Hazarth In high school our teacher taught us about memory in the computer and how to reference it in an array. We did this through sorting algorithms. So it never felt hard. However, I have worked with very intelligent people who had strange views of how pointers actually work.
  • 3
    The concept is simple but just like templates in C++, a clever person can write really unreadable code with pointers.
  • 1
    @nitnip SFINAE magic is the bane of my existence. Sure it's fast, but most of the time it's write-only code.
  • 1
    I was an assembly language programmer before I learned C. “Oh, pointers are addresses” and it clicked.
Add Comment