What can a pointer of type void be used for?

Hi.. I want to know what a void pointer could be used for and if anyone could post some links to examples where void pointers are being used.
closed account (Dy7SLyTq)
its c's version of auto i believe
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.

For example,
1
2
3
4
5
6
7
8
9
void some_func(void *data) {
    Foo *v = (Foo*) data;
    v->do_stuff();
}

int main() {
    Foo f;
    some_func( (void*) f );
}


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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
g_boolean callback( ClutterActor *actor, ClutterEvent* event, void* data) {
      
    SomeDataStruct s = (SomeDataStruct*) data;
    s->do_something();

    return TRUE;
}

int main() {
    SomeDataStruct s;
    ClutterActor* actor = clutter_actor_new();
    g_signal_connect(actor, "button-press-event", callback, (void*)s); 
    clutter_main_enter();
}


In C++ you don't want to use void*, unless you're using a C library that requires using it.
Last edited on
@hitirwin

Why not
1
2
3
4
5
6
7
g_boolean callback( ClutterActor *actor, ClutterEvent* event, SomeDataStruct* s) {
      
//    SomeDataStruct s = (SomeDataStruct*) data;
    s->do_something();

    return TRUE;
}
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;
}


This is the declaration of pthread_create;

 
int pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);


void*(*)(void*) is the type of the parameter, which is a function pointer to a function which takes a void* as an argument and returns a void*.

Last edited on
Topic archived. No new replies allowed.