C++ 什么是 void* 以及它可以指向哪些变量/对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5104640/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:24:47  来源:igfitidea点击:

What is void* and to what variables/objects it can point to

c++cios

提问by SNR

Specifically, can it point to int/float etc.? What about objects like NSString and the like? Any examples will be greatly appreciated.

具体来说,它可以指向 int/float 等吗?像 NSString 之类的对象呢?任何例子将不胜感激。

回答by Armen Tsirunyan

void*is such a pointer, that any pointer can be implicitly converted to void*.

void*是这样一个指针,任何指针都可以隐式转换为void*.

For example;

例如;

int* p = new int;
void* pv = p; //OK;
p = pv; //Error, the opposite conversion must be explicit in C++ (in C this is OK too)

Also note that pointers to const cannot be converted to void*without a const_cast

另请注意,指向 const 的指针不能在void*没有const_cast

E.g.

例如

const int * pc = new const int(4);
void * pv = pc; //Error
const void* pcv = pc; //OK

Hth.

嗯。

回答by Benubird

In C any pointer can point to any address in memory, as the type information is in the pointer, not in the target. So an int* is just a pointer to some memory location, which is believed to be an integer. A void* pointer, is just a pointer to a memory location where the type is not defined (could be anything).

在 C 中,任何指针都可以指向内存中的任何地址,因为类型信息在指针中,而不是在目标中。所以 int* 只是一个指向某个内存位置的指针,它被认为是一个整数。void* 指针只是指向未定义类型(可以是任何类型)的内存位置的指针。

Thus any pointer can be cast to void*, but not the other way around, because casting (for example) a void pointer to an int pointer is addinginformation to it - by performing the cast, you are declaring that the target data is integer, so naturally you have to explicitly say this. The other way around, all you are doing is saying that the int pointer is some kind of pointer, which is fine.

因此,任何指针都可以转换为 void*,但不能反过来,因为转换(例如)指向 int 指针的 void 指针是添加信息 - 通过执行转换,您声明目标数据是整数,所以你自然要明确地说出这一点。反过来,你所做的就是说 int 指针是某种指针,这很好。

It's probably the same in C++.

在 C++ 中它可能是相同的。

回答by karlphillip

Besides everything else that was already said by the other users, a void*it's commonly used in callback definitions. This allows your callback to receive user dataof any type, including your own defined objects/structs, which should be casted to the appropriate type before using it:

除了其他用户已经说过的所有其他内容之外,void*它通常用于回调定义中。这允许您的回调接收任何类型的用户数据,包括您自己定义的对象/结构,在使用之前应将其转换为适当的类型:

void my_player_cb(int reason, void* data)
{
    Player_t* player = (Player_t*)data;

    if (reason == END_OF_FILE)
    {    
        if (player->playing)
        {
          // execute stop(), release allocated resources and 
          // start() playing the next file on the list
        }
    }
}

回答by unwind

A void *can point at any data-like thing in memory, like an integer value, a struct, or whatever.

Avoid *可以指向内存中任何类似数据的东西,例如整数值、结构体或其他任何东西。

Do note, however, that you cannot freely convert between void *and function pointers. This is because on some architectures, code is not in the same address space as data, and thus it's possible that address 0x00000000 for code refers to a different set of bits than address 0x00000000 for data does.

但是请注意,您不能在void *函数指针和函数指针之间自由转换。这是因为在某些体系结构上,代码与数据不在同一地址空间中,因此代码的地址 0x00000000 可能指代与数据的地址 0x00000000 不同的一组位。

It would be possible to implement the compiler so that void *is large enough to remember the difference, but in general I think this is not done, instead the language leaves it undefined.

可以实现编译器,使其void *足够大以记住差异,但总的来说,我认为这并没有完成,而是语言未定义它。

On typical/mainstream computers, code and data reside in the same address space, and then the compilers typically generate sensible results if you do store a function pointer into a void *, since it can be quite useful.

在典型/主流计算机上,代码和数据驻留在相同的地址空间中,如果将函数指针存储到 a 中void *,编译器通常会生成合理的结果,因为它非常有用。

回答by Bernd Elkemann

void*can point to an address in memorybut the syntax has no type-information. you can cast it to any pointer-type you want but it is your responsibility that that type matches the semantics of the data.

void*可以指向内存中的地址但语法没有类型信息。您可以将其转换为您想要的任何指针类型,但您有责任确保该类型与数据的语义相匹配。