Search code examples
c#.netwinapipinvoke

Pointer to another pointer (levels of indirections)?


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?


Solution

  • 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.