Disadvantages of using void * pointers in C

There are many disadvantages to use void *

in C (memory, type, efficiency) .... Regardless, we use them for the flexibility they provide.

List the drawbacks / drawbacks using void *

(and the preferred C solution - if possible).

EDIT: Please go to the following link: http://attractivechaos.wordpress.com/2008/10/02/using-void-in-generic-c-programming-may-be-inefficient/

-2


source to share


6 answers


No performance issues with void pointers. The only restrictions with void pointers are:

  • You cannot expand a void pointer for obvious reasons.
  • sizeof(void)

    is illegal
  • you cannot do pointer arithmetic on void pointers


However, GCC assumes it sizeof(void)

is 1 and allows you to draw arithmetic on void pointers - see here

+14


source


I disagree with the premise of the question. We use void * in C because that's the only way to get polymorphism. Example: library functions qsort and bsearch. There is only one drawback: void * based polymorphism is unsafe: after you have cast a pointer to void *, there is nothing to prevent you from casting that void * on the wrong pointer type error. My students often make this mistake.

The cost of efficiency can be affected because sometimes a lot of space needs to be allocated to use a polymorphic data structure.



Anyone who wants to see the benefits and tradeoffs of using polymorphic data structures with void * should get a copy of Dave Hanson's book C Interfaces and Implementation

+14


source


Uh ... I'm not sure there are that many. Of course, you should never use void*

when you don't need it. If there is a well-defined type that you can use, then do so.

In my experience, void*

it works best for "anonymous" pointers (what a shock!), Both in the return value malloc()

, and also when dealing with opaque bit buffers. Often you want to refer to these buffers, for example. byte level, and then you would of course use unsigned char *

.

Just random use void*

wherever you need a pointer will just break, smelly code, and should be avoided of course.

+3


source


The linked post compares operations on void pointers to operations on C ++ templates and concludes that templates are more efficient. This is hardly a surprise, and the C ++ code I've seen rarely or never uses void pointers. It usually has no advantage over other C ++ objects and can be a gaping hole in the type system.

However, C has no C ++ style templates, and void pointers are required to implement functionality that is data type independent.

So when you're writing in C and you need versatility, void pointers are the most efficient way to get it (since that's the only way). When you write in C ++, there are better ways to do almost everything that void pointers can do, so don't use them.

+2


source


Your link is partly true. Especially when you are not dealing with objects (structures) or in general with types that have not yet been allocated. Using native types like int, double, etc. And void pointers (for a container, for example) are almost always a bad thing to do, because you have the choice to force int (for a double, it doesn't work) to a pointer, or you need to allocate additional memory for the data type.

The first choice is bad because it is not portable, 0s may not be allowed as a value, and it just feels bad. The second choice wastes memory and really (massive) slows down due to extra allocations.

But most of the time you are not dealing with native types, but with objects, better talking to pointers to objects that are already allocated, at least I do. I never need a hash table or card for integers or doubles. And having different container implementations just for the pointer safety type seems wrong to me because each implementation will increase your binary size. Therefore, if you only need a container to store pointers, there is no slowdown or memory loss in using void pointers.

But note that it was about implementing containers, like the blog article you mentioned. In general, there are many things that you cannot accomplish without using void pointers.

+1


source


I don't know, I've found that void pointers are pretty efficient for accessing various levels of abstraction (ABC). As a means of navigating related classes at different levels of abstraction. It's that simple, it's amazing. Like the formula for e or the golden ratio, there must be an occult that worships emptiness * which is great :)

+1


source







All Articles