I have been learning C++ recently, and upon reading up on pointers I had a moment of thought.

I'm still attempting to grasp the very idea of pointers so excuse me if this doesn't make sense beyond comprehension.

Is it possible for a set of pointers recursively point into one another's memory location upon trying to print out the value of what they point at.

This should be done without needing to create this in a "fake" way of creating them in a never ending loop.

It should indefinitely point into infinity when trying to access a pointer's pointed at value. Is this a feasible "thing" to create?

  • 3
  • Yes - a pointer is the address of a memory location. The memory location could contain any kind of data, including the address of a different memory location, i.e. a new pointer. So you could have a recursive structure in memory like this. But you will not get an infinite loop just by fetching the value at the pointer address, since you only fetch "one step", even if the value you fetch is a new pointer. So the result is a pointer value (a 64 number), and you can print a this without problems. Only if you in code recursively follow the pointers, will you fall into an infinite loop. – JacquesB Jan 21 '16 at 9:48

That's not how pointers work.

A pointer is, essentially, a record of a memory address. If dereferenced, you can read (or write) the value that lives at that address.

Pointers can point to anything that lives in memory. In particular, they can point to other pointers. They can even point themselves (although that is pretty useless).

However, the action of dereferencing a pointer must be taken explicitly. A pointer that points to another pointer yields the value of that other pointer when you dereference it, not automatically. And if that other pointer points to a third thing, reaching that third thing takes two explicit actions.

A pointer doesn't magically turn into the thing pointed to in your hands. Therefore it isn't possible to create reference structures that compute forever without having an explicit loop in the mix. Such a loop can run endlessly, like any loop with an insufficient termination condition, but you do need the loop, not just the pointers.

| improve this answer | |

A very schematic explanation

Basic Idea: A pointer is a variable storing a memory address (typically 4 bytes)


char* t_pointer; // This Pointer is a 4 bytes variable that will contain (but currently does not contain) the memory address of a char 

Setting up a pointer value: A typical way consists of using the “&” address-of or reference operator applied to a variable


char t_char = ‘a’; // A 1 byte variable containing character `a`
char* t_pointer = &t_char; // The pointer now contains the memory address of the `t_char` variable 

Getting the value of the pointed element: The “*” dereference operator is used so the syntax (*t_pointer) returns the content of the memory address stored in t_pointer

Two observations:

  • there is no restriction about the memory address the pointer can store so it can point to a valid memory region, even itself, as well as an invalid memory region Using the dereference operator involves trying to access that memory region and that’s where issues can happen

  • C and C++ are typed languages so you should understand what’s the type of the value accessed by the dereference operator In the above example, the t_pointer is defined as a (char*) so it means that when you dereference it you get char … this means that while dereferencing you are basically reading one byte only as sizeof(char) is 1 In case of int* t_pointer then dereferencing it would caused reading sizeof(int) that’s typically 4 bytes

Finally consider that you are not strongly constrained by the pointer type as you can perform casting


void* t_pointer; // Pointer to an undefined type 
int t_int = 11; // Int Variable 
t_pointer = &t_int; // Memory address of the first byte of the int

variable int t_res = (int)(*t_pointer); // Dereference the pointer specifying to read an int and store that value in a new variable

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.