What I wonder is if there are any pitfalls/ issues of doing this?
You need to be absolutely sure while casting the the void*
back to the particular type, if you don't, you end up with an Undefined behavior and a potential disaster. Once you use void *
you lose type safety.It is difficult to keep track of what type a void *
is actually pointing to, there is no way to guarantee or determine that it indeed points to the type to which you are going to typecast it back to.
Is there any way that we could run in to an error or undefined condition when we are casting to void* and back again?
Yes, the scenario mentioned in #1
.
How should we resolve such issues if there are any?
Avoid using void *
in C++ completely, instead use templates and inheritance.
In C you might absoultely need it in certain situations but try to keep its use to a minimum.
Bottomline,
C/C++ allows you to shoot yourself in foot, it is up to you to do or not do so.