Pertinence of void pointers
Looking through a colleague's code, I see that some of its handles are stored as void pointers.
// Class header void* hSomeSdk; // Class implementation hSomeSdk = new SomeSDK(...); ((SomeSDK*)hSomeSdk)->DoSomeWork();
Now I know that sometimes handles are void pointers because it may be unknown before runtime what will be the actual type of the handle. Or that it can help when we need to share the pointer without revealing its actual structure. But this does not seem to be the case in my situation: it will always be SomeSDK and it is not shared outside the class where it is created. Also the author of this code is gone from the company.
Are there other reasons why it would make sense to have it be a void pointer?
Since this is a member variable, I'm gonna go out on a limb and say your colleague wanted to minimize dependencies. Including the header for
SomeSDK is probably undesirable just to define a pointer. The colleague may have had one of two reasons as far as I can see from the code you show:
- They just didn't know they can add a forward declarations like
class SomeSDK;to allow defining pointers. Some programmers just aren't aware of it.
- They couldn't forward declare it. If
SomeSDKis not a class, but a type alias (aka
typedef), then it's not possible to forward declare it exactly. One can only declare the class it aliases, but that in turn may be an implementation detail that's hard to keep track of. Even the standard library has a similar problem, that is why it provides
iosfwdto make forward declaring standard stream types easier.
If the code is peppered with casts of this handle, then the design should have been reworked ages ago. Otherwise, if it's in one place (or a few at most) only, I can see why the people maintaining it could live with it peacefully.
Use of Void pointers in C programming language, Here comes the importance of a “void pointer”. A void pointer is nothing but a pointer variable declared using the reserved word in C 'void'. Ex:- void *ptr; // Now A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typcasted to any type. // typecasted to any type like int *, char *, .. Note that the above program compiles in C, but doesn’t compile in C++.
If I had to guess, the ex-colleague was unfamiliar with forward declarations and thus didn't know they could still do
SomeSDK* in the header without including the entire
Given the constraints you've mentioned, the only thing this pattern achieves is to eliminate some type safety, make the code harder to read/maintain, and generate a Stack Overflow question.
What was the need for a void pointer?, int var1;. ptr=&var1; // This is invalid because 'ptr' is a character pointer variable. Here comes the importance of a “void pointer A void pointer can point to a variable of any data type and void pointer can be assigned to a pointer of any type. Dereferencing a void Pointer. We can’t just dereference a void pointer using indirection (*) operator. For example:
void* were popular and needed back in C. They are convenient in the sense that they can be easily cast to anything. If you need to cast from
char*, you have to make a mid cast to
The problem with
void* is that they are too flexible: they do not convey intentions of the writer, making them very unsafe especially in big projects.
In Object Oriented Design it is popular to create abstract interface classes (all members are virtual and not implemented) and make pointers to such classes and then instantiate various possible implementation depending on the usage.
However, nowadays, it is more recommended to work with templates (main advantage of C++ over other languages), as those are much faster and enable more compile-time optimization than OOD allowed. Unfortunately, working with templates is still a huge hassle - they have more complicated syntax and it is difficult to convey intentions of the writer to users about restrictions and demands of the template parameters (Concepts TS that solves this problem decently will be available in C++20 - currently there is only SFINAE, a horrible temporary solution from 20 years ago; while Reflection TS, that will greatly enhance generic programming in C++, is unlikely to be available even in C++23).
The Important But Restricted Role Void Pointers Should Play in Your , The malloc() library function is an example of a function that returns a void pointer since it does not know what sort of objects will be stored in the memory it No, void indicates the absence of type, it is not something you can dereference or assign to. is there is any way of generalizing a function which can receive pointer and store it in void pointer and by using that void pointer we can make a generalized function.. You cannot just dereference it in a portable way, as it may not be properly aligned.
C void pointer, advantage and usage. – ggirjau, char *ptr; int var1; ptr = &var1; // This is invalid because 'ptr' is a character pointer variable. Here comes the importance of a “void pointer”. A void A void pointer is a pointer that can point to any type of object, but does not know what type of object it points to. A void pointer must be explicitly cast into another type of pointer to be dereferenced. A null pointer is a pointer that does not point to an address. A void pointer can be a null pointer.
void pointer in C/C++, you should know, C void pointer has no associated data type. It can store Let see an example code to understand the working of qsort and importance of void pointer: In this Void is used as a keyword. The void pointer, also known as the generic pointer, is a special type of pointer that can be pointed at objects of any data type! A void pointer is declared like a normal pointer, using the void keyword as the pointer’s type: General Syntax: void* pointer_variable; void *pVoid; // pVoid is a void pointer
void pointer in C / C++, A void pointer is a pointer that has no associated data type with it. 2) void pointers in C are used to implement generic functions in C. For example compare Your original attempt is incorrect in "formal" C, since pointer arithmetic is not applicable to void * pointers. However, some compilers support pointer arithmetic on void * pointers as a non-standard extension (GCC being a notable example). In such compilers void * pointers are treated as char * pointers for arithmetic purposes. And your
- If it's internal to the class, then there is no good reason at all. A handle is a nebulous concept.
hSomeSdkis a member of a class by any chance? Some programmers just aren't aware they can forward declare classes in order to define pointer to them. The colleague may have wanted to avoid including a header they otherwise don't need in your own header file.
- I think the easiest way to understand the reason for the void pointer in this case would be to ask your colleague why they are using a void pointer there. Stands out like a sore thumb.
- @StoryTeller It is indeed a member.
- @AlexMillette Based solely on the shown code, it's not so sad.