什么是 C++ 中的空指针?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8530080/
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 a void pointer in C++?
提问by zeboidlund
Possible Duplicate:
What is a void pointer and what is a null pointer?
可能的重复:
什么是空指针,什么是空指针?
I often see code which resembles something like the following:
我经常看到类似于以下内容的代码:
void * foo(int bar);
void * foo(int bar);
What does this mean? Does it mean that it can return anything? Is this similar to dynamic
or object
in C#?
这是什么意思?这是否意味着它可以返回任何东西?这与C#相似dynamic
还是object
在 C# 中?
回答by Nicol Bolas
A void*
does not mean anything. It is a pointer, but the type that it points to is not known.
Avoid*
不代表什么。它是一个指针,但它指向的类型未知。
It's not that it can return "anything". A function that returns a void*
generally is doing one of the following:
并不是它可以返回“任何东西”。返回 a 的函数void*
通常执行以下操作之一:
- It is dealing in unformatted memory. This is what
operator new
andmalloc
return: a pointer to a block of memory of a certain size. Since the memory does not have a type (because it does not have a properly constructed object in it yet), it is typeless. IE:void
. - It is an opaque handle; it references a created object without naming a specific type. Code that does this is generally poorly formed, since this is better done by forward declaring a struct/class and simply not providing a public definition for it. Because then, at least it has a real type.
- It returns a pointer to storage that contains an object of a known type. However, that API is used to deal with objects of a wide variety of types, so the exact type that a particular call returns cannot be known at compile time. Therefore, there will be some documentation explaining when it stores which kinds of objects, and therefore which type you can safely cast it to.
- 它正在处理未格式化的内存。这是
operator new
和malloc
回报:一个指向一定大小的内存块。由于内存没有类型(因为它还没有正确构造的对象),所以它是无类型的。伊:void
。 - 它是一个不透明的手柄;它引用创建的对象而不命名特定类型。执行此操作的代码通常格式不佳,因为通过前向声明结构/类并简单地不为其提供公共定义可以更好地完成此操作。因为那样,至少它有一个真正的类型。
- 它返回一个指向包含已知类型对象的存储的指针。但是,该 API 用于处理各种类型的对象,因此在编译时无法知道特定调用返回的确切类型。因此,将有一些文档解释它何时存储哪些类型的对象,以及您可以安全地将其转换为哪种类型。
This construct is nothinglike dynamic
or object
in C#. Those tools actually know what the original type is; void*
does not. This makes it far more dangerous than any of those, because it is very easy to get it wrong, and there's no way to ask if a particular usage is the right one.
此结构是什么样dynamic
或object
在C#。这些工具实际上知道原始类型是什么;void*
没有。这使得它比任何一种都更危险,因为它很容易出错,而且没有办法询问特定用法是否正确。
And on a personal note, if you see code that uses void*
's "often", you should rethink what code you're looking at. void*
usage, especiallyin C++, should be rare, used primary for dealing in raw memory.
就个人而言,如果您看到void*
“经常”使用's 的代码,您应该重新考虑您正在查看的代码。void*
用法,尤其是在 C++ 中,应该很少见,主要用于处理原始内存。
回答by User1988
Void is used as a keyword. The voidpointer, also known as the generic pointer, is a special type of pointer that can be pointed at objects of any data type! A void pointer is declared like a normal pointer, using the void keyword as the pointer's type:
void 用作关键字。该空指针,也被称为通用指针,指针是一种特殊类型,它可以在任何数据类型的对象指出!void 指针像普通指针一样声明,使用 void 关键字作为指针类型:
General Syntax:
一般语法:
void* pointer_variable;
void *pVoid; // pVoid is a void pointer
A void pointer can point to objects of any data type:
void 指针可以指向任何数据类型的对象:
int nValue;
float fValue;
struct Something
{
int nValue;
float fValue;
};
Something sValue;
void *pVoid;
pVoid = &nValue; // valid
pVoid = &fValue; // valid
pVoid = &sValue; // valid
However, because the void pointer does not know what type of object it is pointing to, it can not be dereferenced! Rather, the void pointer must first be explicitly cast to another pointer type before it is dereferenced.
但是,因为void指针不知道它指向的是什么类型的对象,所以不能解引用!相反,在取消引用之前,必须首先将 void 指针显式转换为另一种指针类型。
int nValue = 5;
void *pVoid = &nValue;
// can not dereference pVoid because it is a void pointer
int *pInt = static_cast<int*>(pVoid); // cast from void* to int*
cout << *pInt << endl; // can dereference pInt
Source: link
来源:链接
回答by Mark Ransom
A void*
pointer is used when you want to indicate a pointer to a hunk of memory without specifying the type. C's malloc
returns such a pointer, expecting you to cast it to a particular type immediately. It really isn't useful until you cast it to another pointer type. You're expected to know which type to cast it to, the compiler has no reflection capability to know what the underlying type should be.
一void*
,当你想表示一个指向的内存大块没有指定类型的指针被使用。Cmalloc
返回这样一个指针,期望您立即将其强制转换为特定类型。在您将其转换为另一种指针类型之前,它确实没有用。您应该知道将其强制转换为哪种类型,编译器没有反射能力来知道底层类型应该是什么。
回答by axon
A void* can point to anything (it's a raw pointer without any type info).
void* 可以指向任何东西(它是一个没有任何类型信息的原始指针)。