drawbacks of a void pointer in C++ [closed]

被刻印的时光 ゝ 提交于 2019-12-07 15:20:42

问题


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?


回答1:


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.




回答2:


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.)




回答3:


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*.




回答4:


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.




回答5:


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




回答6:


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.




回答7:


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.



来源:https://stackoverflow.com/questions/14961755/drawbacks-of-a-void-pointer-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!