11

I am trying to wrap my head about a void* and what is appropriate use of it and if there is any potentially abusive use I should know about. (Meaning something cool, not wrong).

I dont understand how and why to use a void*. If I am understanding I would need to cast my current pointer to a void* and then cast back to the original when I wanted to use it...?

Why is this beneficial? Not to make things generic because I need to know what type of void* was passed in to do the cast...

Can anyone help me understand void*

Jasmine
  • 15,375
  • 10
  • 30
  • 48
  • C++ usually has better alternatives, and I know there's a specific dupe of this I'm thinking of. – chris Jun 26 '13 at 21:29
  • 3
    In C++ never use `void *` expect in *very* exceptional circumstances. – Ed Heal Jun 26 '13 at 21:31
  • 1
    It's one of *those* features. If you don't know if you need it, you don't need it. –  Jun 26 '13 at 21:31
  • 2
    If you have an pointer to an object of polymorphic type and want to find out the address of the most-derived object containing it, use a `dynamic_cast` on it. – Kerrek SB Jun 26 '13 at 21:32
  • 1
    Variables of type `void *` are used to address raw memory, and thus they are the standard communication types of `operator new` and `operator delete`. – Kerrek SB Jun 26 '13 at 21:33
  • 1
    `void*` is useful for generic programming in C. Take a look at [qsort](http://en.cppreference.com/w/cpp/algorithm/qsort) and [bsearch](http://en.cppreference.com/w/cpp/algorithm/bsearch). In C++, we have templates, which are better in every single way except one. – Benjamin Lindley Jun 26 '13 at 21:34
  • 2
    Ah, this is what I recalled: http://stackoverflow.com/questions/16866333/void-casting-what-is-this-used-for – chris Jun 26 '13 at 21:34
  • Is it correct to say that the `auto` is supposed to take the place of most usages of `void*` in C++? – daparic Jul 20 '18 at 15:05

2 Answers2

13

In C it's fairly common to use void * to create a generic collection. For example, if you wanted a generic linked list, you might write something like:

typedef struct node  { 
    void *data;
    struct node *next;
} node;

Then you could create one linked list of foo, another linked list of bar, and so on.

In C++, however, this is rarely useful. In a typical case, you'd want to write equivalent code as a template instead:

template <typename T>
class node { 
     T data;
     node *next;
};

With older compilers, you could reduce the generated code by combining the two techniques. In this case, you'd use a collection of pointers to void to store the data, then have a template front-end that just handled casting T * to void * when you stored the data, and back from void * to T * when you retrieved the data.

Improvements in compilers/linkers have rendered this technique (almost?) entirely obsolete though. Older compilers would generate separate code for each type over which you instantiated your template, even where the instantiation type was irrelevant. Current compilers (or toolchains, anyway -- quite a bit of this is really handled by the linker) identify the identical code sequences, and merge them together automatically.

Bottom line: unless you need C compatibility, or you're writing a replacement operator new/operator delete, use of void * in well written C++ is usually pretty rare.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • I suggest using `uint8_t *` instead of `void *` for those platforms whose minimal addressable unit is 8-bits. In my opinion, pointing to an 8-bit unit is better than a pointer to nothing. – Thomas Matthews Jun 27 '13 at 00:13
4

void* is a memory address without a type. It's useful when the code around it has some other way of knowing what type it is working with (for example, memchr needs to have the size of the memory area passed in addition to the address).

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720