Is there ever a time they're necessary or are they a symptom of poor code design? If the former, can you give an example? If the latter, can you explain the dangers of using them?
-
They're often necessary to interface with C API's. – Barmar Sep 09 '15 at 22:08
-
7This smacks of a homework question. Which would be fine if you showed that you'd made some effort, and were perhaps looking for clarification. But that's not evident. There appear also to be multiple questions here. It's one question at a time here on Stack Overflow. – David Heffernan Sep 09 '15 at 22:09
-
They're sometimes handy for type erasure, too. – Cameron Sep 09 '15 at 22:10
-
A void* It is a pointer, but the type that it points to is not known. dealing in unformatted memory. This is what operator new and malloc return: a pointer to a block of memory of a certain size. – ohadsas Sep 09 '15 at 22:13
-
Not homework, I've just found myself in times where I think to use but want to genuinely know more. Thanks for the condescension though! – destrovel Sep 09 '15 at 22:14
-
They can be useful when interfacing with hardware at times to signify a plain ol' memory address. – rlbond Sep 09 '15 at 22:14
-
When using a `DMA` with another device (eg. an FPGA) they are useful to change arbitrary blocks of 32 bit ints into structures that hold usefull data. – Fantastic Mr Fox Sep 09 '15 at 22:16
-
If you want to use `malloc` in your C++ code for any reason, malloc returns a `void *`, so there will be a pointer like that in your program, even if you cast it to another type right away. – Chris Beck Sep 09 '15 at 22:22
-
1What's their utility? For when you don't know/care about the type in memory pointed to. Kinda unnecessary in C++ with templates, but still a means to an end. – RamblingMad Sep 09 '15 at 22:25
-
Sorry again for misreading. See my answer. I've added a small example to illustrate a common scenario along with two things you might want to be careful about when working with void pointers. – rbaleksandar Sep 09 '15 at 22:55
-
possible duplicate of [What is a void pointer and what is a null pointer?](http://stackoverflow.com/questions/4334831/what-is-a-void-pointer-and-what-is-a-null-pointer) – Evan Carslake Sep 09 '15 at 22:56
2 Answers
Void pointers can be viewed as a neutral way of passing things around. A void pointer can point at whatever you want unlike a non-void one.
Example: Imagine you have an integer variable
int myvar;
There are two ways you can tackle this variable if you want to pass it along somewhere using a pointer:
int *myvar_ptr = &myvar;
or
void *myvar_ptr = &myvar;
In the first case using a integer pointer restricts you to pointing at only a memory block containing an integer. In the second case you might as well have a float, double, an object, the beginning of an array of some sort, std container etc.
The downside is that even though you have this neutral way of passing things around you still have to retrieve the thing that the void pointer is pointing at at some point, which means that you need to cast it to the respective type. This is a very tricky part and if you do it incorrectly you can 1)create segmentations faults (imagine your void pointer is pointing at an array of chars but you cast it back to a double -> memory-wise you jump "further" thus the probability of accessing a memory you are not supposed to is pretty big), 2)corrupt your data etc.
Another downside of the void pointers is the absence of pointer arithmetic. You cannot do:
myvar_ptr++;
if myvar_ptr
is a void pointer since you don't know what the +1
indicates memory-wise. Might be a char, might be a big fat object or else.
In C++
most people prefer using pointers combined with templates since you still get a higher degree of flexibility compared to a primitive type pointer yet the information about the type is not lost. Mimicing void pointers for classes using templates combined with pointers is not that difficult especially since we have things inheritance. Void can also be used as a type for a template (see here).
EDIT:
Sorry about misreading your question's title. Since I don't want my writing to go to waste here is the old post.
Some extra information on pointers:
Pointers are useful for
- talking to C applications (especially those using UI APIs where event handling of the various components is usually (always?) done using pointers to functions)
- dynamic memory allocation including malloc (and similar oldies but goldies), new operator (C++; new actually returns a pointer if you didn't know) etc.
- pointer arithmetic - performance and flexibility
- embedded software development - pointers allow you a very precise access to the memory (which is also essential for the *nix systems, where C is the widely used standard language)
- generally offer extra flexibility and reusability of components - unlike some other language C/C++ allows you to pass things by value and by reference, which means that you are not obligated to copy stuff (basically what pass by value does) if you don't want to. Since everything has a starting point in the memory you can even pass functions using pointers (as I've mentioned a very popular way of doing UI components' callbacks)
Pointers can also be a pain in the butt:
- many books/tutorials do a poor job in explaining in an easy-to-understand manner how you can screw things up (pretty badly on top of that) if you use pointers incorrectly
- stacking pointer of a pointer of a pointer of a pointer ... can lead to an extremely obfuscated (meaning hard to read and understand) code
- where there is dynamic memory allocation involved there is always a chance you miss something and there is you big fat memory leak. But then again you can assign something on the stack to a pointer. ;)
In C++ as mentioned by some here manages to hide a lot of the pointer stuff (maybe exactly in order to prevent developers misusing pointers) but you still need it depending on what you are doing.

- 1
- 1

- 8,713
- 7
- 76
- 161
-
Note I was asking about *void* pointers, though this is all good to know. – destrovel Sep 09 '15 at 22:38
-
void pointers are pointers to data of unknown type.
For example, you can write a sorting function for sorting data. You do not need to know the data type (numeric, ASCII text, Chinese, a double precision number or something else), the algorithm is always the same. If you pass in a void pointer to the data and a pointer to a data comparison function you will be able to sort the data.
Another example of using a void pointer would be a data compression function. The compression function doesn't care about the data type. It only needs to know the start of the data and the size of the data.

- 5,510
- 2
- 26
- 26