Disadvantages of void pointer in C ++

As far as I know, a void pointer in C ++ void*

can point to anything. This can be very helpful (for me) if I want to develop a solution without using some sort of inheritance. But the question I want to know is, are there any performance disadvantages to this approach?

+3


source to share


7 replies


The biggest drawback is that using void pointers stops the compiler from being able to type-check. Especially in a language that supports object oriented principles, I think it would be strange to use void-intensive pointers.



You will most likely find void pointers in C to mimic some polymorphic behavior, but the same type safety issues exist.

+9


source


There are two major performance disadvantages void*

. First, it makes it difficult to understand and maintain a program that has a serious impact on maintenance programmer performance and runtime, as it makes it difficult to rewrite the software when the profiler shows you where your performance issues are. The fastest programs are those that are well written first, since then it becomes possible to do local optimizations at critical points without rewriting the entire program.



The second performance impact is alias void*

something. Correct analysis of anti-aliasing is an important part of compiler optimization, and anything you do to do it makes the optimizer more difficult and can lead to slower code. ( char*

and unsigned char*

have a similar effect.)

+5


source


There is void*

no downside to performance when used , other than the compiler cannot make assumptions about the type it points to. It is still a pointer just like any other.

A void*

was useful in C for pointing to any arbitrary type, because any pointer to an object's type can be converted to void*

. However, doing almost nothing with it will result in undefined behavior. You can safely cast it back to the original pointer type.

However, in C ++ we have a much better way to store a pointer to some arbitrary type templates. For some template argument, T

we can have T*

.

+4


source


void*

can point to anything. This can be very helpful (for me) if I want to develop a solution without using any inheritance

If you are thinking about passing a pointer void*

to some function, think about how it will be used. To call a method on this object or access its members, you still need to know the type of that object.

Use void*

only if you really need to. If you're looking for a way to handle objects in a more general way, then just go for proper OO development. When you have a pointer void*

, the only thing you know is that "there is something at this address in memory", nothing more.

The performance of using pointers void*

is the same as with any other pointers. Security , understandability , and the associated extensibility and maintainability of your code should be your concerns.

+1


source


There are still platforms out there that have void*

more than other pointer types or have a different format, so the cast to or from void*

turns into the actual conversion code. On shared hardware (x86, ARM) this is not the case, so void*

there are no performance issues with , you just give up type safety.

+1


source


I don't understand why there are performance issues when using a void pointer and not any other pointer. I would care more about the safety you give up on the void pointer.

0


source


Yes - you may have some performance disadvantages. By using void *

, you can turn off the optimization based on the strict pseudo-reduction assumption.

Example

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

      

If it weren't for the variable void *

, the compiler could have removed assignment 1, and at 2 it would just generate param2 = 8

. But now this is not possible because it param1

can point to param2

if you called fun(&i, i)

.

More about strict alias

0


source







All Articles