C++ 中指向 void 的指针?

发布于 2024-07-10 22:45:48 字数 179 浏览 6 评论 0原文

我正在阅读 Ogre3D 实现中的一些代码,但我无法理解 void *< /code> 类型变量的意思。 指向 void 的指针在 C++ 中意味着什么?

I'm reading some code in the Ogre3D implementation and I can't understand what a void * type variable means. What does a pointer to void mean in C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

清风夜微凉 2024-07-17 22:45:48

指向 void、void* 的指针可以指向任何对象:

int a = 5;
void *p = &a;

double b = 3.14;
p = &b;

您无法取消引用、递增或递减该指针,因为您不知道所指向的类型。 这个想法是,void* 可用于诸如 memcpy 之类的函数,这些函数仅复制内存块,而不关心它们复制的类型。

A pointer to void, void* can point to any object:

int a = 5;
void *p = &a;

double b = 3.14;
p = &b;

You can't dereference, increment or decrement that pointer, because you don't know what type you point to. The idea is that void* can be used for functions like memcpy that just copy memory blocks around, and don't care about the type that they copy.

何止钟意 2024-07-17 22:45:48

它只是一个通用指针,用于在不知道类型时传递数据。 您必须将其转换为正确的类型才能使用它。

It's just a generic pointer, used to pass data when you don't know the type. You have to cast it to the correct type in order to use it.

ゝ杯具 2024-07-17 22:45:48

它是指向内存中某个位置的原始指针。 它不允许任何指针算术,如 char * 或 int *。

以下是一些使用示例

http://theory.uwinnipeg.ca/programming/node87.html

It's a raw pointer to a spot in memory. It doesn't allow any pointer arithmetic like char * or int *.

Here's some examples of usage

http://theory.uwinnipeg.ca/programming/node87.html

百善笑为先 2024-07-17 22:45:48

它是一个指向任何东西的指针——只是你可以使用的一块内存——可能是一个对象,也可能是一个字符。 你必须使用它来做任何有用的事情。

It's a pointer to anything -- just a chunk of memory that you can play with -- might be an object, might be a char. You have to cast to do anything useful with it.

够运 2024-07-17 22:45:48

基于我之前的帖子...


注意:这里的所有内存地址都是虚构的。 我编造它们只是为了说明一点。


假设:

int data[] = {10,11,12};

我们现在有:(

0xffff0000-0xffff0003  with a value of (int)(10)
0xffff0004-0xffff0007  with a value of (int)(11)
0xffff0008-0xffff000b  with a value of (int)(12)

要在这里讨论大端字节序与小端字节序。)

如果我们有:

int * p = data;

我们现在在其他地方有另一个内存位置,说:

 0xaaaa0000-0xaaaa0003  with a value of (int*)0xffff0000

我们可以使用 p[1] [或 *(p + 1)] 将 *(int*)(0xffff0004) [=11] 引用为 sizeof(int)=4 和 0xffff0000+sizeof(int ) = 0xffff0004。

如果我们有:

void * v = data;

我们现在在其他地方有另一个内存位置,则说:

 0xbbbb0000-0xbbbb0003  with a value of (void*)0xffff0000.

但是, void 没有任何关联的 sizeof() 信息。 我们不能增加或减少指针。 我们无法取消引用来访问存储在 0xffff0000 中的数据。 我们只能将该值用作原始内存地址。

如果我们想使用 (void*)0xffff0000 中存储的数据,我们首先需要将其转换为适当的类型。

也就是说,(void *) 作为将地址传递到任意数据结构的方法仍然非常有用。 例如,memset()。 无论我将 struct tm 还是 struct sockaddr 清零并不重要。 我们只需要一个指向结构体及其大小的指针。

(这应该是不言而喻的,但是......请注意使用 memset 将类实例清零,并在这样做时覆盖虚拟指针表。)

Building off my prior posting...


ATTENTION: All memory addresses here are fictional. I'm just making them up to illustrate a point.


Given:

int data[] = {10,11,12};

We now have:

0xffff0000-0xffff0003  with a value of (int)(10)
0xffff0004-0xffff0007  with a value of (int)(11)
0xffff0008-0xffff000b  with a value of (int)(12)

(I'm not going to get into big-endian vs little-endian byte ordering here.)

If we have:

int * p = data;

We now have another memory location somewhere else, say:

 0xaaaa0000-0xaaaa0003  with a value of (int*)0xffff0000

We can use p[1] [or *(p + 1)] to refer to *(int*)(0xffff0004) [=11] as sizeof(int)=4 and 0xffff0000+sizeof(int) = 0xffff0004.

If we have:

void * v = data;

We now have another memory location somewhere else, say:

 0xbbbb0000-0xbbbb0003  with a value of (void*)0xffff0000.

However, void doesn't have any associated sizeof() information. We can't increment or decrement the pointer. We can't dereference to access the data stored in 0xffff0000. We can only utilize the value as a raw memory address.

If we want to use the data stored in (void*)0xffff0000, we first need to cast it to an appropriate type.

That said, (void *) is still quite useful as a means of passing addresses to arbitrary data structures around. For instance, memset(). It doesn't matter whether I'm zero'ing out a struct tm or a struct sockaddr. We just need a pointer to the struct and its size.

(This should go without saying, but... Beware using memset to zero out a class instance and, in doing so, overwriting the virtual pointer table.)

耳根太软 2024-07-17 22:45:48

void 指针不能指向 C++ 中的类成员。

A void pointer cannot point to a class member in C++.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文