3

A far as I know, a void pointer in C++ void* can point to anything. This might be quite useful (for me) if I want to develop a solution without using some sort of inheritance. But the question I want to know is whether there is any performance drawbacks in this approach?

Danny
  • 9,199
  • 16
  • 53
  • 75
  • 4
    What performance drawbacks? It is just a pointer like pointer to other types. From the description of your implementation seems you are trying to solve a C++ problem using C ways, which you should not. If you want inheritance use inheritance not `void *`. – Alok Save Feb 19 '13 at 16:02
  • 3
    The main trouble is figuring out what it actually does point to in order to correctly cast it back. And if you get that wrong, you're lucky if you notice the bugs right away. – aschepler Feb 19 '13 at 16:02
  • 3
    There won't be performance drawbacks, but you lose all type information (and thus type safety). This means the compiler won't be able to spot errors it could otherwise, and you will encounter them at runtime instead (think undefined bejaviour and hard-to-track bugs). – Angew is no longer proud of SO Feb 19 '13 at 16:02
  • 3
    I wouldn't worry about performance drawbacks. The biggest drawback is that pretty much anything you do with a `void*` will give you undefined behaviour. I think the only defined use of a `void*` is to cast it back to point at the correct type. – Joseph Mansfield Feb 19 '13 at 16:02
  • There can easily be performance drawbacks. At some point you have to figure out what the type of the `void*` actually is before using it (unless you are using it as an `intptr_t`). If the type was already encoded in the program, that step would not be required. – Yakk - Adam Nevraumont Feb 19 '13 at 16:08

7 Answers7

9

The biggest drawback is that making use of void pointers stops the compiler from being able to enforce type checking. Especially in a language that supports object oriented principles, I think it would be strange to be making heavy use of void pointers.

You are more likely to find void pointers in C to mimic some polymorphic behavior, but the same type-safety concerns exist.

Gavin H
  • 10,274
  • 3
  • 35
  • 42
5

There are two serious performance drawbacks of void*. The first is that it makes it far more difficult to understand and maintain the program, which has a serious impact on the performance of the maintenance programmers—and on run-time, since it makes reworking the software more difficult when the profiler shows where you have performance problems. The fastest programs are the ones that are well written first, since it then becomes possible to make local optimizations in the critical places without having to rewrite the entire program.

The second performance impact is that a void* can alias anything. Proper aliasing analysis is an important part of compiler optimization, and anything you do to make this more difficult hinders the optimizer, and can result in slower code. (char* and unsigned char* have a similar effect.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
4

There is no performance drawback when using a void* other than the fact that the compiler can't make assumptions about the type it points at. It is still a pointer like any other.

A void* was useful in C for pointing to any arbitrary type because any pointer to object type can be converted to a void*. However, doing pretty much anything with it will result in undefined behaviour. You can only safely cast it back to the original pointer type.

However, in C++ we have a much better way of storing a pointer to some arbitrary type - templates. For some template argument T, we can have a T*.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
1

void* can point to anything. This might be quite useful (for me) if I want to develop a solution without using some sort of inheritance

If you are thinking about passing a void* pointer to some function, think already about the way it will be used. In order to invoke a method on this object or access its members, you need to know the type of that object anyway.

Use void* only if you really have to. If you are looking for a way how to treat objects in more generic way, then just go for proper OO design. When you have a void* pointer, the only thing you know is that "there is something at this address in the memory", nothing more.

Performance of using void* pointers is same as with any other pointers. Safety, understandability and also with it related extensibility and maintainability of your code should be your concerns.

LihO
  • 41,190
  • 11
  • 99
  • 167
1

There may still exist platforms where a void* is larger than other types of pointers, or have a different format so a cast to or from void* is turned into actual conversion code. On common (x86, ARM) hardware, that is not the case, so there are no performance problems with void* -- you're just giving up type safety.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
1

Yes - you can have some performance drawbacks. By using void * you may disable optimization based on strict aliasing assumption.

Example

void fun(void * param1, int & param2)
{
    param2 = 7; // 1
    // do something with param1, param2 not used
    param2 += 1; // 2
}

If there was no void * variable, compiler might remove assignment 1 and in 2 just generate param2 = 8. But now it can't, because param1 may point to param2, if you called fun(&i, i).

More about strict aliasing

Tadeusz Kopec for Ukraine
  • 12,283
  • 6
  • 56
  • 83
0

I can't see why there'd be any performance issues when using a void pointer as opposed to any other kind of pointer. I would be more concerned with safety, which you are giving up with a void pointer.

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91