9

I'm looking through the source of a C# program that uses a library written in C. I came across this line and was unsure what it was:

cvbimUNSAFE.GetImageVPA ((cvbim.IMG)cvImg.Image, 0, (void**)&lpImageBits, &pVPAT);

What is an object of type void **? I did some Google searches and could only find information about void*, which is a pointer to a sort of catch all top level type, if I understood correctly.

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
user12345613
  • 833
  • 9
  • 21
  • I know this comes a bit late, but as a side note: Declaring a parameter as `void**` is also the C equivalent of passing a `void*` by reference or as an out parameter (`void*&`) and this is actually what the function `GetImageVPA` does (I happen to know the library...). So in the DllImport statement for `GetImageVPA` the safest thing to write would be `out IntPtr` or `out void*` (the latter requiring `unsafe` specification). – StuporMundi Jun 07 '17 at 15:18

6 Answers6

18

It's a pointer to a pointer to something not specified. Basically, just think of it as a memory pointer to a raw memory pointer.

So, int** is a pointer to a pointer to an int, but void** is a pointer to a pointer, but it's not specified what that pointer is pointing at.

I did some google searches and could only find information about void*, which is a pointer to a sort of catch all top level type, if I understood correctly.

Not quite. void* is a pointer to something, it's just not specified what that something is and should just be thought of as a pointer to a raw hunk of memory that you have to apply some structure to. For example, malloc returns a void* because it's returning a pointer to a raw hunk of memory.

jason
  • 236,483
  • 35
  • 423
  • 525
2

It's a void pointer. See this article for details:

http://msdn.microsoft.com/en-us/library/y31yhkeb%28VS.80%29.aspx

And you can take a look at this SO question for details on how to implement it in C#:

How to declare a void pointer in C#

On a side note, that method should be marked as unsafe if it's not.

Community
  • 1
  • 1
James Johnson
  • 45,496
  • 8
  • 73
  • 110
1

In this case, I am guessing this library will allocate the amount of memory necessary to hold the image so it needs a double indirection so it can change the address lpImageBits points to.

Vinicius Kamakura
  • 7,665
  • 1
  • 29
  • 43
1

In C, it represents a pointer to a void* object. In other word, when you dereference it you get a void*.

I guess this is used because lpImageBits will be modified inside the function you are invoking.

Simone
  • 11,655
  • 1
  • 30
  • 43
0

In C# void** is a pointer to void* pointer. Here is a simple example:

   public unsafe static void Main(string[] args)
   {
        int x = 1;
        void* v = &x;

        //dereferencing v, it prints 1
        Console.WriteLine(*(int*)v);

        void** vtp = &v;
        //dereferencing vtp, it prints 1
        Console.WriteLine(*(int*)*vtp);

        Console.ReadLine();
    }
V. S.
  • 1,086
  • 14
  • 14
-3

That's not C#. It looks like C++. In that case, a void** is a pointer to a void pointer.

Jez
  • 27,951
  • 32
  • 136
  • 233