Beginner question: In relation to this original question here, every variable we generate has a memory address to it, and the pointer holds that memory address. The pointer itself also has its own memory address. Why doesn't this recursively generate an infinite chain of memory-addresses that hold other memory-addresses until your memory (that holds all these memories) blow up, whenever you declare just a single variable? What am I missing here?
-
3Because it's possible, in multiple ways, for the chain to terminate. It can point to an element that already exists, closing the loop after finitely many elements. It can point to `nullptr`, which doesn't have a pointed-to object and therefore just ends the chain without there being a "next" item. – Nathan Pierson Jun 18 '22 at 03:20
-
What pointer exactly do you mean by "the pointer"? – Joseph Sible-Reinstate Monica Jun 18 '22 at 03:28
-
4Your base assumption is wrong. Not every memory address has other memory pointing at it. Memory can exist without being pointed to. It just is, man. Like, you can't like, OWN a rock, man. – JohnFilleau Jun 18 '22 at 03:29
-
1A pointer is a variable who's value is an address in memory. Not all addresses need to be stored in distinct variables. So `int x = 42; int *p = &x;` means that there are two variables named `x` and `p`, and their values are `42` and `&x` respectively. There is no requirement that there be a third variable (i.e. a pointer) which holds the value of `&p` so (unless code explicitly creates a variable to hold `&p`) the chain you describe is broken at that point. – Peter Jun 18 '22 at 03:39
-
1But you can smell what the rock is cooking. – user4581301 Jun 18 '22 at 03:40
-
Thank you for the explanation! I have a clear idea right now where the chain gets broken. And I'm certain for real, no rocks after that =) – Samuel Low Jun 18 '22 at 06:02
1 Answers
What am I missing here?
The names of the pointers.
An expression like &(&a)
is not valid; there needs to be a name for an address before you can take the address again. (There are more precise ways to express that, but the more precise ways can also be more confusing at this stage.)
In the linked-to question, there is a variable with a name.
int a = 10;
This variable has a value (a
) and an address (&a
). However, the address is not itself necessarily stored in memory until it is given a name.
int *p = &a;
At this point, there is the value a
, the address of that value (&a
stored in p
), and the address of a pointer to that value (&p
). However, the address of p
is not necessarily stored in memory until it is given a name.
int **q = &p;
And so on.
Yes, you could theoretically have an unbounded chain of these names, but each name is necessary. If you stop generating names, you stop getting things to take the address of. Even if you find a way to get the compiler to generate names for you (so you don't have to type each out, which would take forever), the compiler will stop generating those names once it hits one of its internal limits. So, possible in theory, but not in practice.

- 14,422
- 4
- 15
- 31
-
_the address is not itself stored in memory_. Where is it stored, then? I mean, that address, even if I don't give it a name, is somewhere, in the sense that I can interact with it, for instance by printing it, or by adding it a number and so on. – Enlico Jun 18 '22 at 04:26
-
I'd expect it to exist in a register, rather than in main memory. Maybe in a CPU cache. But you are right, I did simplify the wording down to the level I perceived the question to be at. Hmm... adding "necessarily" might make the answer more correct without sacrificing clarity. – JaMiT Jun 18 '22 at 04:27
-
Thank you for the time to explain this crystal clear! So the lack of the name basically breaks the chain =) gotcha – Samuel Low Jun 18 '22 at 06:04