18

I have been on Reddit...
I have been lurking in ProgrammerHumor...

I am not proud of these things...

I got called a "Big Shot" because I didn't think the concept of pointers in C/C++ was ever particularly hard.

If I remember right. I learned in high school how pointers worked when they explained how arrays worked in Pascal. When I taught myself C it didn't ever seem like it was a difficult thing to understand.

Is the concept of pointers really that hard to understand for devs?

Comments
  • 11
    I guess a lot of programmers don't want to care about memory, thus everything from allocation to destruction seems like a pain.
    In my opinion it's a basic necessity to know how pointers and references work, independent of a programming language.
  • 8
    C's pointers are incredibly simple technology with crazy complex implications because they introduce the notion of ownership as a whole design concern. C++ introduces that concept into the language through smart pointers, which makes the technology itself at least as complex as that concept, plus whatever difference between C++'s designers' approach to ownership and yours.
  • 5
    It has to do with short / long term memory.

    I know a lot of people who struggle remembering things - either because they have the attention span of a 4 year old on chocolate covered espresso beans or don't like to take notes... Or in it's worst they're lazy bums.

    The hard thing is usually not the concept behind pointers - it's more puzzling together the full context and why the pointer makes sense at this point in the global context and (in C case) what happens to the pointer during it's lifetime.

    Applies to various other things, too.

    E.g. HTTP and TCP. If I got a penny for every time I need to explain that receiving an response does neither mean that the connection in TCP is closed nor that immediately a new connection is started... Oh Lord. I would be richest man alive.

    Small things that are not hard to remember and one _could_ immediately realize on their own with a bit of effort and brain capacity.

    If anyone feels offended... Have a cookie.
  • 1
    @IntrusionCM The concept of async temporal displacement is definitely much harder to grasp than locations in memory. I see people trying to run code in threads and wonder why the thread is not done 2 lines of code later. Again I gotta whip out the boxes.
  • 5
    IMO, pointers are very easy to understand and work with.

    Yet I’m very glad that I don’t need to use them in higher languages.

    It’s just so much more secure and convenient to let the compiler do the pointer work.
  • 1
    @Lensflare I was using Oblivion scripting. It had references and no pointers (IIRC). I still was able to create memory leaks.
  • 4
    @Demolishun of course it’s possible to create memory leaks without pointers.
  • 7
    @IntrusionCM I agree, using a few pointers within a function or small program is quite straight forward.

    Using data structure with nested pointers that live for a long time, is used by more than one thread and that needs freeing up, that can get complex very quickly even for a short program.

    Freeing to early, of forgetting to free up one nested pointer, or forgetting to update a pointer so it uses the former value or ….

    There are many ways to mess up, once you find them its usually obvious.

    And while direct pointer manipulation can be very fast, the real cases where you need it and can do a better job than the compiler are quite rare unless your building some seriously advanced stuff.
  • 2
    most of the "devs" you encounter there are python or js script kiddies.
  • 3
    @Voxera I think it is probably more the object lifetime that is difficult. I see people launch async routines with objects created on the stack. Then wonder why the object never notified them when the async routine completed. Well, because the object was destroyed as soon as they leave the instigating function.
  • 1
    @nitnip I am starting to realize at lot of subreddits are low effort posts.
  • 5
    Pointers are a simple concept. (Yet it kinda starts hurting my brain trying to think of them due to problems in my short-term/working memory.) I'm still clad I have most of the memory management done for me. xD

    Trying to remember the syntax to everything after half a day of not working with it... I'm currently working as an Angular developer and I need to google/see reference in code for ngIf/ngFor syntax every time I want to use it. >.>

    Concepts are eaaasy, if I just could write pseudo all day. Maybe I could switch to Python.
  • 0
    @Demolishun References are Pointers. That is how they are implemented.
  • 0
    Once you understand it, it is easy. For me it is very easy.

    Just like driving a bicycle, once you learned it, it is easy but you need some time to learn it first.
  • 1
    I also don't know what the deal is with new programmers thinking that pointers are difficult. They are not. Literally just a place in memory reference. If you can imagine the memory as long row of bytes. than you can imagine a variable holding the index of a specific cell in the row... Pointers are dumb simple as a concept...

    However truly mastering their management and knowing the tricks you can do with them... that's slightly more complex and mostly just a thing you have to learn to keep in the back of your mind at all times... Though we also have tools now like Valgrind that can help you discover leaks quite easily
  • 2
    I guess the hard part as a beginner is using them. Like, passing by-value or by-reference, then de-/dede-/referencing at the right place and then dealing with all that errors can be a PITA. Also the whole stack/heap and "did it change my variable or a copy of it?" shit.
    The concept is damn simple

    Might also be a meme feedback loop (looking at you r/programmerHumor - who tf struggles with ; nowadays?)
  • 1
    @Jedidja those problems only occur in languages like Java and C#. Because references/values and heap/stack is tied to the kind of type that is used: class or struct or whatever.

    C and C++ on the other hand is pretty straight forward. Whatever the thing is that you are copying, it’s always clear what happens. If it’s a pointer, you are copying the pointer (by reference). If it’s not a pointer, you are copying by value. Everything lives on the stack, even pointers. Heap allocations are always explicit via malloc or new. The pointers on the stack are pointing to values on the heap.

    There are no special rules and exceptions to remember unlike in other languages. Everything can be derived and explained from those simple rules.

    When beginners have problems with pointers, I think it’s because they have worked with other languages before where the rules are complex.
Add Comment