What is PVOID data type?

2020-02-23 07:52发布

Can someone explain what PVOID is and how it is used in a function like:

BOOL DoSomething(PVOID pMemPhy)

标签: c++ pointers com
6条回答
一夜七次
2楼-- · 2020-02-23 08:08
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.

查看更多
Summer. ? 凉城
3楼-- · 2020-02-23 08:10

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.

查看更多
相关推荐>>
4楼-- · 2020-02-23 08:18

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.

查看更多
啃猪蹄的小仙女
5楼-- · 2020-02-23 08:25

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

查看更多
虎瘦雄心在
6楼-- · 2020-02-23 08:30

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

查看更多
啃猪蹄的小仙女
7楼-- · 2020-02-23 08:31

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;
}
查看更多
登录 后发表回答