1

I have one inquiry. This topic is well-known by many, but I have came across it just recently and I have maybe basic question.

On this site, we can read about memory indirection.

From that site, I have an understanding that one level of undirection matches reference types, that is variable of reference type has just a pointer to somewhere in memory, where object is allocated. That is how I understood level of indirectrion.

Question: Is this understanding correct?

If no, what would be the correct understanidng? And what would be two level of inderection?

If yes, what would be the second level of indirection? Would it be pointer to another pointer? Is that so?

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • A pointer is itself just a variable (or object allocated at some location in memory). Thus a second level of indirection is simply a pointer to an object that happens to be a pointer to some other object. I think you've mostly got it. – siride Sep 09 '18 at 15:47
  • 1
    Is is like passing the water bucket from hand to hand. Number of people is the number of indirection. https://www.electrosmash.com/images/tech/mn3007/fire-bucket-brigade.jpg – 0___________ Sep 09 '18 at 15:49
  • See: [What does “level of indirection” mean in David Wheeler's aphorism?](https://stackoverflow.com/a/18003704/880990) – Olivier Jacot-Descombes Sep 09 '18 at 15:53

1 Answers1

4

You have the examples provided for a second level of indirection. In C# a signature like this:

DoWork(ref MyType x)

requires a reference to a reference type. Which is equivalent (as stated in the article you linked) to an unmanaged signature of

DoWork(MyType** x);

So a pointer to a pointer.

Your understanding is correct - a reference type provides at least one level of indirection, as it's a pointer to an object. A ref MyType x is a reference to a reference type, so it's a pointer to a pointer to an object, and thus a second level of indirection.

V0ldek
  • 9,623
  • 1
  • 26
  • 57