Can someone explain what PVOID
is and how it is used in a function like:
BOOL DoSomething(PVOID pMemPhy)
Can someone explain what PVOID
is and how it is used in a function like:
BOOL DoSomething(PVOID pMemPhy)
void pointer, same as
void *pMemPhy
aka "Pointer to something, but it's up to you to figure it out".
BOOL DoSomething ( PVOID pMemPhy )
{
strcpy((char *)pMemPhy, "I love buffer overflows!");
return TRUE;
}
It's a void pointer -- a pointer to a memory address with no information about the type of the value that it is pointing to. For this reason, you must cast the pointer to a type such as (char *)pMemPhy
or (int *)pMemPhy
before using the pointer so that the compiler knows how much memory it's working with (1 byte for a char, 4 bytes for an int, etc.)
As others have said, it's equivalent to void *
.
void
pointers are often used in memory operations (memcpy
, memset
, etc...) since one doesn't want to assume the type of the data at the given address.
Pointers and void pointers are given a good treatment in this article from http://cplusplus.com.
This and other mnemonic like BOOL, LPCTSTR have it origin with Windows, which BTW was developed before the existence of a C standard, and to not depend in a particular compiler it used its own types.
You can check the Old New Thing blog for more stories about to the Windows development history, and its oddities which will remain with us (http://blogs.msdn.com/oldnewthing).
typedef void * PVOID;
If you're question is what use is a void pointer? The most common use is when you're passing a pointer to memory that doesn't really care about type. free(), for example.
If a library exports a function that can take multiple pointer types, but wants to support languages like C that don't have function overloading, then void * works.
It sounds like it's just an alias (define or typedef) for void*. I have no idea why people think that would be better but I know some APIs like to use that in case the implementation of a type changes in future.
I know that early versions of windows used things like STDCALL as a prefix to many functions and the definition of STDCALL could change based on which version of Windows you compiled for. This is from memory (which is affected by alcohol after many years :-), so don't rely on this as gospel. It's basically correct but the details might be a little different.