C++ 什么是 PVOID 数据类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/494163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is PVOID data type?
提问by geofftnz
Can someone explain what PVOID
is and how it is used in a function like:
有人可以解释一下PVOID
它是什么以及如何在函数中使用它,例如:
BOOL DoSomething(PVOID pMemPhy)
回答by geofftnz
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;
}
回答by Jeremy Ruten
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.)
它是一个空指针——一个指向内存地址的指针,没有关于它所指向的值的类型的信息。出于这个原因,您必须将指针转换为一个类型,例如(char *)pMemPhy
或(int *)pMemPhy
在使用指针之前,以便编译器知道它正在使用多少内存(char 为 1 个字节,int 为 4 个字节,等等)
回答by Gordon Wilson
As others have said, it's equivalent to void *
.
正如其他人所说,它相当于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.
void
指针通常用于内存操作(memcpy
、memset
等...),因为人们不想假设给定地址处的数据类型。
Pointers and void pointers are given a good treatment in thisarticle from http://cplusplus.com.
来自http://cplusplus.com 的这篇文章很好地处理了指针和空指针。
回答by Ismael
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.
这个和其他助记符如 BOOL、LPCTSTR 起源于 Windows,顺便说一句,它是在 C 标准存在之前开发的,并且不依赖于使用自己类型的特定编译器。
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).
您可以查看 Old New Thing 博客,了解更多关于 Windows 开发历史及其奇怪之处的故事,这些故事将留在我们身边 ( http://blogs.msdn.com/oldnewthing)。
回答by Die in Sente
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.
如果库导出一个可以采用多种指针类型的函数,但想要支持像 C 这样没有函数重载的语言,那么 void * 可以工作。
回答by paxdiablo
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.
听起来它只是 void* 的别名(define 或 typedef)。我不知道为什么人们认为这会更好,但我知道有些 API 喜欢使用它,以防将来类型的实现发生变化。
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.
我知道早期版本的 Windows 使用 STDCALL 之类的东西作为许多函数的前缀,并且 STDCALL 的定义可能会根据您编译的 Windows 版本而改变。这是凭记忆(多年后受酒精影响:-),所以不要将其视为福音。它基本上是正确的,但细节可能有点不同。