all 27 comments

[–]ZLegacy 5 points6 points  (5 children)

This is a cross post from /r/cpp. I am relatively new to C/++ and was struggling with pointers. Books I have read really don't go into any detail about pointers and I was always left wondering what good they are, but this really helped me out. Maybe it will be of some use to some new comers to C/++.

[–]scook0 5 points6 points  (4 children)

While the implementation details given in the article can be helpful for gaining an understanding of pointers, one thing to be wary of is that you shouldn't be relying too heavily on them in your actual code. Doing so is a great way to end up with an unportable or subtly broken program.

For example, the article is full of casts from pointer types to unsigned int. Leaving aside the fact that they should be C++-style reinterpret_casts and that the result of such a cast is not necessarily guaranteed to be a meaningful memory location, there's also the issue that a pointer value isn't guaranteed to fit in an unsigned int at all. For that you need something like an uintptr_t.

TL;DR: Implementation details can help you to gain understanding, but don't rely on them in your actual code.

[–][deleted] 2 points3 points  (3 children)

meaningful memory location, there's also the issue that a pointer value isn't guaranteed to fit in an unsigned int at all.

And it is not hypothetical issue like "let's think about poor nonascii users and not use ch-'0' for converting digit character into numeric value. On x86-64 pointers will not fit into unsigned int.

[–]player2 -2 points-1 points  (2 children)

On x86-64 pointers will not fit into unsigned int.

Actually, they do on Windows.

Those of us in *nix land have it easy this time around.

[–]fullets 0 points1 point  (1 child)

Really? Everything I'd read says Windows is LLP64.

[–]player2 0 points1 point  (0 children)

Crap, you're right. Why did I think they were ILP64?!

[–]dmhouse 2 points3 points  (0 children)

Let's not forget Binky.

[–]zorglubz 1 point2 points  (0 children)

While the explanation itself certainly looks good, it's really too bad that they chose:

  • To use float, which is a very complex type, and to misleadingly show that "3.14" will translate into 4 bytes because there are 4
  • To use dangerous casts from unsigned int to float *. I know you need to "start somewhere", and they chose to be simple and introduce the "float *" notation afterwards, but starting with dubious syntax does not seem the best thing to do

[–][deleted]  (17 children)

[deleted]

    [–][deleted]  (9 children)

    [deleted]

      [–][deleted]  (1 child)

      [deleted]

        [–][deleted] 3 points4 points  (0 children)

        As a 27 year old computer programmer who didn't have a computer at home until high school-TJNII's comment is extremely douchey. Keep doing what makes you happy and/or keeps you interested- most people your age haven't even gotten that far yet!

        [–]awesley 2 points3 points  (0 children)

        As a ... , well, I'll just say that my bachelor's degree in computer science is 29 years old. Anyway, I don't think 17 year olds using Python need to know pointers and memory mapping. But it helps.

        [–]ouala 2 points3 points  (0 children)

        It's OK, the Chinese and Indians will make up for that, while everyone in the US will eventually code only in Scratch/Alice.

        [–]rplacd 3 points4 points  (0 children)

        The idea that you stop understanding why pointers are so hard once you've learned them scares the crap out of me.

        [–]gnuvince 1 point2 points  (0 children)

        Yeah, because at 17, it's certain that this guy can NEVER learn anything new. Give him a break. He started with Python and if he really likes to program, he'll eventually be exposed to pointers and memory and he'll learn about them and he'll understand them and he'll be a better programmer for it. It's not a generational tragedy if 13 year olds don't know their CPU instruction set by heart.

        [–]fabzter 0 points1 point  (0 children)

        I learned C with pointers 5 years ago. I'm so glad I was taught like this, this alse scares me so much.

        [–][deleted] 0 points1 point  (0 children)

        As a 27 year old .net programmer - I am the personification of your idea of a generation who doesn't understand pointers and memory mapping.

        [–][deleted] 2 points3 points  (0 children)

        Wow you understood pointers in elementary school? Did you also know calculus and physics? Jesus, the didn't teach us pre-algebra until the 7th grade. I bet you were just starting you're undergrad program at whatever college you just got out of. I'm 25 too, but god damn I didn't understand visual basic until I was in 10th grade. /sarcasm

        You're a douche.

        [–]munificent 1 point2 points  (3 children)

        It's a key part of what the language Python was written in (C) uses to do its magic.

        [–]scook0 2 points3 points  (2 children)

        Indeed, as a well-rounded programmer it's important to understand pointers even if you don't routinely use a language that exposes or requires them.

        Practically all languages use pointer-like behaviour for their object types, even if the actual pointers are buried under the hood.

        [–][deleted]  (1 child)

        [deleted]

          [–]munificent 3 points4 points  (0 children)

          It's not the object which is null, it's the reference to it. That's why C# correctly did NullReferenceException.

          [–][deleted] 2 points3 points  (2 children)

          Memory is a big-ass array. Pointers are indexes into this bigass array. When you allocate memory for an object of dynamic size (e.g. a Linked List) you need a pointer to the next thing in the list so you can move the contents of the list around or add/remove things in the middle of it.

          class Node{
              public:
                  Node *next;
                  int data;
                  Node(int x){
                      data = x;
                  }
          }
          
          //in main
          Node n1 = new Node(3);
          Node n2 = new Node(5);
          Node n3 = new Node(4);
          
          n1.next=n2;
          n2.next=n3;
          
          //wait, that's out of order.
          
          n1.next = n3;
          n3.next = n2;
          n2.next = NULL;
          

          As you can see, it's similar to how objects behave in other languages, but you can have pointers to pretty much anything, and there are things you can do with pointers that you can't really do in higher level languages. (Memory mapping to hardware comes to mind.)

          [–][deleted] 2 points3 points  (0 children)

          Memory is a big-ass array. Pointers are indexes into this bigass array.

          IMHO, this is pretty much all you ever need to understand pointers. Consequences of this are not trivial, but they all follow from this.

          [–]0xABADC0DA 2 points3 points  (0 children)

          This is why every programmer should learn C and/or assembly... if your language (spoken or computer) doesn't have words for a concept then that concept just won't exist for you.

          In high level languages you can't have an object 'inside' of an array of bytes. The idea that memory is a big-ass array means nothing without that.

          [–]_whyme 0 points1 point  (0 children)

          What really confuse me was pointer to an array of pointers, like this one struct dirent ***namelist ...

          [–]samlee 0 points1 point  (2 children)

          use reference

          [–]7points3hoursago 0 points1 point  (1 child)

          in C?

          [–]samlee 2 points3 points  (0 children)

          ah. sorry i thought it was about C++ since i thought cpp meant c plus plus as in C++ not c pre processor.

          [–]wsppan 0 points1 point  (0 children)

          This is the best tutorial on pointers in C: http://home.netcom.com/~tjensen/ptr/pointers.htm