Sign in to follow this  

What's the void for?

This topic is 4665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone, I was wondering, what's this (void**) for? What does it do?
if(FAILED(pDSB->QueryInterface(IID_IDirectSoundBuffer8,                          (void**)&g_pDSBuffer))) {
      // Error occurred - free first interface first 
      // and then do something
      pDSB->Release();
    } else {

Share this post


Link to post
Share on other sites
it's pointing to a pointer so that the function can use this double-indirection to create memory for the pointer.

the function could probably read as


void foo(void **ppSth)
{
...

*ppSth = new DATA_TYPE;

...
}


Share this post


Link to post
Share on other sites
void is a type that means 'no type'. For example, void foo(void); means that the function neither takes nor return anything (in C++, it's equivalent to void foo();). You can also explicitely cast the result of a function to void to explicitely tell the compiler you haven't forgotten to use it (some compilers used to warn you when you did that): (void) bar(42);.

If you create a pointer to void (i.e. a void*), you have a pointer to some block of memory, without assuming anything about what's held in that memory (as opposed to, say, int*, where you know you're pointing to one or more ints). For example, that's what the C malloc and free functions manipulate: they allocate and release blocks of memory, but know nothing about how they're going to be used.

void* has the interesting property that any type of pointer (int*, double*, sauerkraut*...) can be converted to void*. That's why functions like, for example, the C qsort() function take such void* (so that they'll accept any pointer - in C++, you would use templates, preserving the type right into the function, as std::sort() does).

On the other hand, there isn't much you can do with a void* without casting it back to some other pointer type. To start with, you can't dereference it (i.e. do void* ptr; ... *ptr), since the result would be of type void ... which you can't do anything with. Also, obviously, since a void has no size, you can't do pointer arithmetic on a void*. In fact, the only thing you can do with a void* is pass it around and cast it back to what it was originally (if it was something else, and not the result of malloc()) or to something else (don't make any assumption on the data the memory block used to contain!!!).

A void** is simply a pointer to a void*.

In C (and C-style C++), when a function needs to modify one of its parameters, you need to pass a pointer to it. C only passes parameters by value, so if you were to pass, say, an int and modify it in the function, the int variable outside wouldn't be affected. By passing the address of that variable (void foo(int* ptr); ... int var; foo(&var);), the function can modify the data the pointer points to (*ptr = 42;) and thus affect the variable outside of the function (var becomes 42). In C++, you would use a reference parameter instead (void foo(int& ref); ... int var; foo(var); ... ref = 42;). It's simpler and more flexible.

In your example, g_pDSBuffer is a pointer of some type (let's say a foo*), you take its address (yielding a foo**), and convert it to a void** to match the interface of the QueryInterface function (I don't think there is an automatic conversion to void** as there is to void*).

You will have to study how QueryInterface really works (and how COM works in general) if you want more details about why.

Share this post


Link to post
Share on other sites

This topic is 4665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this