If I am not wrong, it is different from auto in the sense that a void pointer can be made to point to different types of objects in sequence. Also, it cannot be dereferenced directly.
It is normally recommended to avoid using void pointers because by definition they are not type safe and may result in unexpected program behaviour if the programmer is not careful enough.
I do not know whether there exists a situation where using void pointers is the best bet.
Definitely not auto; auto generates an exact known type. Void can hold a raw memory address lacking ordinary type information.
In C, malloc() has to return a void pointer. In C++ new allocates memory like malloc(), calls constructor of the type, and returns the pointer casted to desired type. How the compiler implements it, is not our business. Even new and plain pointers are less and less daily, for C++ has STL containers, smart pointers, and whatnot.
In C it's used sometimes used to allow you to pass any type to a function that must have a fixed set of parameters, like a callback function from a library.
You can let a void* point to any type, this way you can pass any type to a function which takes a void* as a parameter, but you need to cast it back to it's actual type before using it or bad things can happen.
The clutter library for example in C, uses void* like this.
Because the clutter api requires that the callback function has a certain signature.
Clutter is an event driven graphics library with an internal main loop. When you call g_signal_connect, you're setting up a call back function to handle events. It's required that the function pointer argument matches the function pointer parameter to g_signal_connect which is declared in a library header file.
pthreads uses void* like this as well.
1 2 3 4 5 6 7 8 9 10 11 12 13
void* funct(void* data) {
int x = (int*)data;
printf("%d\n", *x);
}
int main() {
int x = 5;
pthread_t t;
pthread_create(&t, NULL, funct, (void*)(&x)));
pthread_join(t, NULL);
return NULL;
}