4 ms·
> Pointers are variables that hold a reference to something in memory. This sentence is extremely confusing or meaningless. I would rather say: "A pointer is
by enriquto 6y ago
> Pointers are variables that hold a reference to something in memory.
This sentence is extremely confusing or meaningless. I would rather say: "A pointer is an integer that indicates a position in the RAM."
Thus, if a pointer "p" indicates a position in memory, then "p+1" is a pointer that indicates the next position in the memory. The value on that cell of memory is often written as p[1].
- efxz 6y agoFor a noob like me, Your explanation is even more confusing. Sorry.
- speedgoose 6y agoYour version isn't very true though. Most operating systems have virtual memory.
- Koshkin 6y agoSome things should be left out until the kids grow up a little. (Otherwise you would have to describe gruesome details of swapping and memory mapping and how dereferencing a pointer might trigger reading from disk etc.)
- frabert 6y agoA newcomer can assume virtual memory = physical memory without any problem. Hell, anyone who's not doing kernel dev can do that. I do it all the time.
- speedgoose 6y agoI usually prefer to not mention something immediately rather than teaching something wrong.
- enriquto 6y agoThis is like explaining general relativity to people who don't know yet newtonian mechanics.
- julian_t 6y agoYou need a bit more than that if people are going to understand you... what is a "position", and what does "next position" mean?
- pingyong 6y agoI've always explained it to people like this: Imagine a big cabinet, like a bunch of safe-deposit boxes in a bank. Each box has an unique number written on it, and the boxes are simply numbered 0 to 99. Now when you open a box, you find a piece of paper in it with a number between 0 to 99. It tells you which box to open next - so it points to another box. That's a pointer. Seems to work pretty well, at least for conveying the basic idea of pointers and dereferencing.
- enriquto 6y agoBut why do you need to mention that this number is inside a box? It seems strangely contrived. Even if the number was outside the box it would be a pointer, it has nothing to do with being in the box.
- pingyong 6y agoBecause usually if I want to store something I put it inside the box? Not sure how that is contrived.
- enriquto 6y agoWhen you explain floating point numbers you also put them into boxes? No need for that, you can explain them intrinsically, just like pointers. Moreover, in practice, it is clear to think that you already have your pointer in your hands (e.g. in a register), no need for your pointer to come from the RAM that it is used refers to. This is an unnecessary recursivity.
- pingyong 6y ago>When you explain floating point numbers you also put them into boxes? That makes no sense. If I was explaining floating point numbers with boxes, for whatever reason, I'd put the values inside the boxes, yes. The analogy just doesn't work here, but the analogy for memory makes perfect sense. >This is an unnecessary recursivity. No, it's not. Think of a linked list for example. Traversing it makes perfect sense with my analogy: You open the first box, look at the number that's inside, open the box corresponding to that number, look at what number is inside that box, open the next corresponding box, etc. - it all makes immediate and intuitive sense. If you just start with "open box x" (because x you somehow already know magically because it's already in a register), you haven't really explained much. Or think of pointers to pointers. With my analogy: Easy! It's just another box which contains the value of the next box. If this concept isn't explained, a pointer to a pointer makes no sense because the pointer value has no corresponding box, hence no address.
- Koshkin 6y agoWait till they start learning about C++. A notion of ‘reference’ there that is separate from that of ‘pointer‘ confuses things even more.
- pjmlp 6y agoNot sure why it should be confusing it goes all the way back to Algol and the languages derived from it on the Pascal/Mesa/Modula/ML family branches.
- AnimalMuppet 6y agoIt took me a minute to recall what you're referring to. To save everyone else the trouble: Pascal had pointers. And it also had var parameters, which are essentially pass-by-reference. (At least, I presume that's what pjmlp is, um, referring to...)
- pjmlp 6y agoNot only, in Ada, Modulas and Oberons you can have them as local variables as well, and in the variants with GC support, they split further into traced and untraced references.
- Koshkin 6y agoBecause in C these notions coincide.
- pjmlp 6y agoReferences are just implicit pointers, it is so hard to get them? Not sure what is so hard, then again I learned them in Basic and Pascal variants before getting to learn C.
- Koshkin 6y agoOK, first of all, the syntax and the semantics of a pointer and a reference are completely different from each other. Departing from C, one must start thinking of a pointer as a variable containing a value that is a reference, the latter now always being a constant. That's pretty subtle, I must say. Add to that a very important distinction between lvalue, rvalue, and "universal" references, and then one needs to stay very much current as a C++ coder to remember all that.
- scoutt 6y ago> A pointer is an integer that indicates a position in RAM, ROM, NVRAM, flash, etc. A pointer stores a number that represents an address, without making reference where is located. Can be 0 (NULL). "p+1" can be the next byte in RAM, or the next 4KB if p is of type "4KB struct".
- blondin 6y agoyes, i find OP's definition very clear and by saying number instead of integer you made it even better. for example i was re-reading some math stuff yesterday that made me realize that sometimes we want natural number instead of integer which can be negative...
- SAI_Peregrinus 6y agoNULL doesn't strictly have to be 0. It usually is, but it could be defined differently depending on the architecture.
- enriquto 6y ago> A pointer stores a number Why not "is" instead of "stores" ?
- scoutt 6y agoA variable that is a pointer occupies space in memory. In that memory (the pointer of the pointer) is stored the number. char* ptr = (char*) 0x80000000; By doing `&ptr` you can know the address of the pointer `ptr`. In that address, the number 0x80000000 is stored.
- necheffa 6y agoTrouble is pointers don't have to be integers. The standard lets you address by fruit and veggies if you have such a machine.
- enriquto 6y agoHowever they are implemented internally is a detail. Formally they are "affine" integers, in that you can compute the sum of an integer and a pointer to obtain another pointer, and compute the difference of two pointers to obtain an integer. I agree that "a pointer is an integer" is an over-simplification. But it is a very useful one when you are learning C. Then you will have time later to enter into the gory details, once you fully master the simple case.
- jcelerier 6y ago> and compute the difference of two pointers to obtain an integer. This is only a legal operation if those two pointers are part of the same object / allocation. It's UB (see C11 6.5.6 Additive operators ; C++ is similar) to do for instance char* ptr1 = malloc(10); char* ptr2 = malloc(10); ptr2 - ptr1; so they don't really behave like any algebraic definition of integers in that regard. Even just comparing them with (ptr1 < ptr2) is UB.
- leni536 6y agoIn C++ comparing them is unspecified, and theoretically can even change from invocation to invocation. But it's not UB. http://eel.is/c++draft/expr.rel#4.3.sentence-1 http://eel.is/c++draft/expr.rel#4.3.sentence-1 std::less is blessed by the standard to work with any two pointers, so relational containers work with pointers.
- bubblethink 6y agoThe other annoyance is the unsinged v/s signed mess in going from size_t to ptrdiff_t.
- 6y ago
- ChrisSD 6y agoThat ends up being misleading though as it ignores optimizing compilers. A pointer isn't simply an integer address of memory. It's tagged (by the compiler) with information about e.g. which allocation unit it came from and what extent it has (i.e. how far you can move the pointer forwards or backwards). The long and short of it is that doing `p+1` can be UB. Also two pointers can point to the same actual memory address but not be equal. Pointers are, in practice, a complex abstraction. It can be hard to understand all the subtleties, especially if you're used to the simplicity of memory addresses in assembly.