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++?
6 Answers
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.

- 496,577
- 130
- 894
- 1,212
-
waw - I didn't realize you couldn't increment a void* ! thanks! – xtofl Jan 04 '09 at 18:37
-
Quite a few compilers will let you - they assume that all pointer types are 32bit. This is wrong but common on Windows where people assume a 32bit x86. – Martin Beckett Jan 04 '09 at 21:26
-
@mgb: Never ever seen such a compiler. – dalle Jan 04 '09 at 23:22
-
I saw one that treated it like char* for arithmatic, which makes sense when you think about it. – Joshua Jan 04 '09 at 23:26
-
2i don't think it makes sense. why should p++; increment it by one? could aswell increment by the pagesize or some other value. since sizeof(*p) is Big Fail, i'm lost at seeing how it could ever make sense :) – Johannes Schaub - litb Jan 05 '09 at 00:02
-
example compiler: gcc allows void pointer arithmetic and treats it like char*. Such code is nonportable, but possible in GNU-land. – Michael Ekstrand Sep 10 '09 at 14:09
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.

- 234,037
- 30
- 302
- 389
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

- 73,752
- 17
- 161
- 228
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.

- 37,700
- 14
- 97
- 166
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.)
A void pointer cannot point to a class member in C++.

- 28,545
- 49
- 129
- 149
-
i think that is because member pointers are completely different structured. If you want, then you can say they live in another address space (and then the reason is the same as the reason you cannot point to a function using `void*`). For sizes, a data member pointer on GCC (following the Itanium ABI) will have the size of a `ptrdiff_t` - it equals the size of an `int*` on my PC - the address space is the offset from the start of the object to its last member offset. For pointers to member functions, the size is usually bigger, in addition, of course. – Johannes Schaub - litb Sep 10 '09 at 17:23