Why MS uses a "pointer to pointer"?
HBITMAP CreateDIBSection(
HDC hdc, // handle to DC
CONST BITMAPINFO *pbmi, // bitmap data
UINT iUsage, // data type indicator
VOID **ppvBits, // bit values
HANDLE hSection, // handle to file mapping object
DWORD dwOffset // offset to bitmap bit values
);
This is my question, use just a pointer can make such a API runs well, but MS uses **ppvBits. There must be something I don't know, tell me if you get the answer, please!
[535 byte] By [
joezhou] at [2007-11-17 22:36:39]

# 2 Re: Why MS uses a "pointer to pointer"?
The problem is that the API must allocate memory for you. The memory allocation function will create a pointer that you as the caller do not yet have. So the the callee needs a pointer to memory to write the new pointer into. Simply using a pointer would overwrite the pointer value of the caller.
Example:
Caller code:
void** ppVoid = NULL;
AllocateSomething (ppVoid,100);
Callee code:
void AllocateSomething (void** ppVoid,dwSize)
{
if (ppVoid)
{
*ppVoid = new BYTE [dwSize];
}
}
You see there is no way to do this using a simple pointer since the allocation would overwrite the pointer value the caller stores on the stack.
Monday at 2007-11-10 8:42:27 >

# 3 Re: Why MS uses a "pointer to pointer"?
hi,
i am begineer in windows programming. May be i am wrong in my explanation to this problem. Please correct me if i am.
Windows uses the concept of handles ( pointer to pointer ). Windows internally maintains a table named as handle table.
when we create some object than windows create a handle internally for it, our object point to these handles . Windows do it because it needs to shuffle the contents of memory locations. It also updates the handle table.
So our pointers does not turned to invalid.
thats all from my side.
# 5 Re: Why MS uses a "pointer to pointer"?
I think Monday's example is incorrect, It should be
Caller code:
void* pVoid = NULL;
AllocateSomething (&pVoid,100);
Callee code:
void AllocateSomething (void** ppVoid,dwSize)
{
if (ppVoid)
{
*ppVoid = new BYTE [dwSize];
}
}