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栏);
What does this mean? Does it mean that it can return anything? Is this similar to dynamic
or object
in C#?
这是什么意思?它是否意味着它可以返回任何东西?这与c#中的动态或对象相似吗?
4 个解决方案
#1
72
A void*
does not mean anything. It is a pointer, but the type that it points to is not known.
void*没有任何意义。它是一个指针,但是它指向的类型是未知的。
It's not that it can return "anything". A function that returns a void*
generally is doing one of the following:
这并不是说它可以返回“任何东西”。返回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
. - 它处理的是未格式化的内存。这是操作符new和malloc返回的内容:指向特定大小内存块的指针。由于内存没有类型(因为它还没有适当构造的对象),所以它是无类型的。即:空白。
- 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.
- 它是一个不透明的把手;它引用一个创建的对象,而不指定特定类型。这样做的代码通常是很糟糕的,因为这样做最好是向前声明一个struct/类,而不是为它提供一个公共定义。因为,至少它有一个真实的类型。
- It has explicit documentation telling you what type(s) that you can use the pointer for.
- 它有显式文档告诉您可以使用指针的类型(s)。
It is nothing like dynamic
or object
in C#. Those constructs 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.
它不像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, especially in C++, should be rare, used primary for dealing in raw memory.
而且,在个人笔记上,如果你经常看到使用void*的代码,你应该重新思考一下你在看什么代码。无效*使用,特别是在c++中,应该很少见,主要用于处理原始内存。
#2
23
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*指针。C的malloc返回这样一个指针,期望您立即将其转换为特定类型。在将它转换为另一个指针类型之前,它确实是没有用的。您应该知道要将其转换到哪个类型,编译器没有反射能力来知道底层类型应该是什么。
#3
22
Void is used as a keyword. The void pointer, 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关键字作为指针的类型:
General Syntax:
一般的语法:
void* pointer_variable;
void *pVoid; // pVoid is a void pointer
A void pointer can point to objects of any data type:
空指针可以指向任何数据类型的对象:
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指针不知道它指向的对象的类型,所以它不能被取消引用!相反,空指针必须首先被显式地转换为另一个指针类型,然后才被取消。
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
#4
4
A void* can point to anything (it's a raw pointer without any type info).
void*可以指向任何东西(它是一个没有任何类型信息的原始指针)。
#1
72
A void*
does not mean anything. It is a pointer, but the type that it points to is not known.
void*没有任何意义。它是一个指针,但是它指向的类型是未知的。
It's not that it can return "anything". A function that returns a void*
generally is doing one of the following:
这并不是说它可以返回“任何东西”。返回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
. - 它处理的是未格式化的内存。这是操作符new和malloc返回的内容:指向特定大小内存块的指针。由于内存没有类型(因为它还没有适当构造的对象),所以它是无类型的。即:空白。
- 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.
- 它是一个不透明的把手;它引用一个创建的对象,而不指定特定类型。这样做的代码通常是很糟糕的,因为这样做最好是向前声明一个struct/类,而不是为它提供一个公共定义。因为,至少它有一个真实的类型。
- It has explicit documentation telling you what type(s) that you can use the pointer for.
- 它有显式文档告诉您可以使用指针的类型(s)。
It is nothing like dynamic
or object
in C#. Those constructs 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.
它不像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, especially in C++, should be rare, used primary for dealing in raw memory.
而且,在个人笔记上,如果你经常看到使用void*的代码,你应该重新思考一下你在看什么代码。无效*使用,特别是在c++中,应该很少见,主要用于处理原始内存。
#2
23
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*指针。C的malloc返回这样一个指针,期望您立即将其转换为特定类型。在将它转换为另一个指针类型之前,它确实是没有用的。您应该知道要将其转换到哪个类型,编译器没有反射能力来知道底层类型应该是什么。
#3
22
Void is used as a keyword. The void pointer, 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关键字作为指针的类型:
General Syntax:
一般的语法:
void* pointer_variable;
void *pVoid; // pVoid is a void pointer
A void pointer can point to objects of any data type:
空指针可以指向任何数据类型的对象:
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指针不知道它指向的对象的类型,所以它不能被取消引用!相反,空指针必须首先被显式地转换为另一个指针类型,然后才被取消。
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
#4
4
A void* can point to anything (it's a raw pointer without any type info).
void*可以指向任何东西(它是一个没有任何类型信息的原始指针)。