c++ arithmetic - When to use a void pointer?

of dereferencing (12)

I understand the use of void pointer for malloc implementation.

void* malloc  ( size_t size );

Can anyone suggest other reasons or provide some scenarios where it is useful in practice.



It is commonly used in numerical code, for example a C root solver function might look like that:

double find_root(double x0, double (*f)(double, void*), void* params)
/* stuff */
y = f(x, params);
/* other stuff */

params is cast by f to some structure it knows about, but find_root doesn't.

Void pointers are useful when you write code that needs to run on multiple operating systems and needs to be fairly agnostic of underlying framework APIs.

For example, OS X, Windows and Linux all have the basic concept of a window object, but they're all very different. So I have common code that passes them around as void*'s and then platform specific implementations that cast the void* to the native type (HWND, etc).

But, yeah, as others have said in this thread, this sort of thing is certainly to be avoided except where necessary.

Another example of such C "generics", implemented with void *, is a standard qsort function:

void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));

You can sort an array of any type: int, long, double, char * or some struct pointers...

In C++, I've found the most compelling use case for void* pointers is to give code the option to store arbitrary "user data" on an object they are already using.

Let's say you've written a class representing a Car, for use in software which does useful things with Car objects (traffic simulation, rental car inventory, whatever). Now let's say you find yourself in a situation where your application wants to keep track of the arbitrary contents of the trunk of a Car. The details of what's stored in the trunk are not important to the Car class, and could be anything -- it really depends on the purpose of the application using the Car class. Enter the void* pointer.

class Car

        // Existing methods of your Car class

        void setContentsOfTrunk(void* contentsOfTrunk);
        void* contentsOfTrunk() const;


        void* m_contentsOfTrunk;

Now, any application using your Car class has the option to attach an arbitrary data object to an existing Car object such that it can be obtained from any code which has the Car object. The contents of the trunk "travel with" the Car object, wherever it goes in your code.

There are two alternatives to using void* in this case.

The first is to template your class based on the type of the trunk contents object:

template <class TrunkContentsType>
class Car

        // Existing methods of your Car class

        void setContentsOfTrunk(TrunkContentsType contentsOfTrunk);
        TrunkContentsType contentsOfTrunk() const;


        TrunkContentsType m_contentsOfTrunk;

This seems unnecessarily invasive. The type of the contents of the trunk are important only to the application. Algorithms and data structures working with Car objects don't care what's in the trunk. By templating the class, you're forcing applications using the class to choose a type for trunk contents, but in many cases the applications don't care about trunk contents either.

The second alternative is to derive a new class from Car which adds a data member and accessors for trunk contents:

class Car

        // Existing methods of your Car class
        // No methods having anything to do with trunk contents.


        // No data member representing trunk contents.

class CarWithTrunkContents

        // Existing methods of your Car class

        void setContentsOfTrunk(TrunkContentsType contentsOfTrunk);
        TrunkContentsType contentsOfTrunk() const;


        TrunkContentsType m_contentsOfTrunk;

The new CarWithTrunkContents class is an application-specific class which adds a data member of the type the application needs to store trunk contents on the car. This also seems unnecessarily heavyweight. Why do you have to derive a whole new class to add an additional piece of data which doesn't affect the behavior of the class? And if it's fairly common for applications using the Car class to want to store trunk contents, why force each application to derive a new class for their particular type of trunk contents?

Finally, while my contrived example of trunk contents maybe paints a vivid picture of arbitrary trunk contents traveling with the Car object, in practice you would likely provide an even more general mechanism for attaching application-specific data to the Car:

class Car

        // Existing methods of your Car class

        void setUserData(void* userData);
        void* userData() const;


        void* m_userData;

This way, an application can attach an object representing trunk contents, or an object representing driver's license and registration, or an object representing rental agreement, or whatever. I've seen this kind of void* pointer referred to as "userData" (i.e. understood by the user of the class), "blindData" (i.e. the class is blind to the contents of the object it carries) or "applicationData" (i.e. data of type and purpose defined by the application).

Next to interfacing with C, I find myself only using void pointers when I need to debug / trace some code and like to know the address of a certain pointer.

SomeClass * myInstance;
// ...
std::clog << std::hex << static_cast< void* >(myInstance) << std::endl;

Will print something like


And, in my opinion, nicely documents what I'm trying to do (know the pointer address, not anything about the instance)

If you are interfacing with C code and need to pass through a C++ object, but a C library will only take a generic pointer, then when you retrieve the pointer you need to re-cast it to the proper type.

Void pointers probably shouldn't be used very often, but they can help when you're trying to use a library function that works with arbitrary pointers, and doesn't really care what data is represented by that memory.

void pointers should be used any time the contents of a block of data is not important. For example when copying data the contents of a memory area is copied but the format of the data is not important.

For functions that operate on blocks of memory without needing to understand the contents using void pointers clarifies the design to users so that they know the function does not care for any data format. Often functions a coded to take a char * to handle blocks of memory when the function is actually content agnostic.

There is great advantage of using void pointer . Pointer variable is an variable which stores the address of another variable. example:

int a;
int *x= &a;

Now 'x' stores the address of the integer variable.

But this one fails:

float f;
int *x = &f;

Because Integer pointer variable can store only integer variable address. in same manner it applies for other data types.

When you use void * pointer , it gives an edge to store address of any TYPE variable.

void *pointer = &i;
void *pointer = &f;

while retrieving it has to be deferenced.


So, carefully make use of void pointer.

This might help you, Thank You.

A GREAT way to learn all about void * and other C topics is to watch the first half of the fantastic Stanford "Programming Paradigms" on iTunes-U. It really explains void * (C generics) and pointers in general fantastically! It definately helped me learn C better...

One of the biggest uses is to use void * if you want to be able to accept different types of data in a function. (heres an example:

Here's a further example of what you can use them for:

  int i;
  char c;
  void *the_data;

  i = 6;
  c = 'a';

  the_data = &i;
  printf("the_data points to the integer value %d\n", *(int*) the_data);

  the_data = &c;
  printf("the_data now points to the character %c\n", *(char*) the_data);

If you don't want to watch the free stanford classes, i'd recommend googling void pointer and reading all the material there.

void * is really a C-ism, and allows C to do some things that it could not reasonably do otherwise.

char * cannot portably be used for anything, as different platforms can make different types of pointers - a char * isn't necessarily handled the same (or is even the same size) as a void *.

So when the type of data isn't known in C (or is polymorphic or otherwise dynamic), then void * allows you to generate the correct underlying pointer type - one that can point to anything correctly.

In C++ void * generally should never come up except in the context of interfacing with legacy C code in one form or another.

int (*f) (void);
f =(void*) getprocaddress(dll,"myfunction");

to make the compiler happy

Apart from syntactic sugar, a reference is a const pointer (not pointer to a const). You must establish what it refers to when you declare the reference variable, and you cannot change it later.

Update: now that I think about it some more, there is an important difference.

A const pointer's target can be replaced by taking its address and using a const cast.

A reference's target cannot be replaced in any way short of UB.

This should permit the compiler to do more optimization on a reference.

c++ c pointers