Skip to main content
GameDev.net gamedev.net
🔒 Locked

void pointers

Started by Hassanbasil Mar 22, 2010 at 11:15 AM 22 replies 3.2k views
Original Post
Hassanbasil
Hassanbasil
can void pointers point to a class/struct/union? if so, shouldn't this work?

//prototype
void ControlCamera ( void *smth );

//in main function
hxCamera *cam = new hxCamera ( 1, 1, 20000 );
ControlCamera ( &cam );

//function body
void ControlCamera ( void *smth )
{
	hxCamera* cm;
	cm = (hxCamera*)smth;

	//do whatever..
}



jpetrie
jpetrie
Quote:

can void pointers point to a class/struct/union?

Yes.

Quote:

if so, shouldn't this work?

No. cam is an hxCamera*, &cam is an hxCamera**, which is then passed to ControlCamera. In ControlCamera you cast the parameter to a hxCamera*.

Don't pass &cam to the function, just pass cam.

Why are you even using void pointers? There are very few good reasons to use them in C++.
SirLuthor
SirLuthor
Aside from "ControlCamera(&cam);" which will have to be "ControlCamera(cam);", your code is technically correct, and will compile.

However, the question you should be asking yourself here is, why am I casting to void * (abandoning the lovely world of type-safety entirely) just to recast to the type in question inside the function? Is there any valid reason for the ControlCamera(...) function not to simply take an hxCamera pointer as an argument? By and large, any use of void pointers within C++ code indicates that you have a problem with your design (pedants note; exceptions exist of course :Þ). If, in fact, there is no alternate way to do what it is you want done within your system, without resorting to a cast to void * and back, then you should probably consider refactoring the section in question. Type safety is one of the many features of C++ in place to prevent you from shooting yourself in the foot, and blindly throwing it away is only going to come back to haunt you later.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Hassanbasil
Hassanbasil
there are actually 2 reasons:
1- like always, practicing
2- this is not the whole function, it's not only taking hxCamera, it will deal with a few more classes, so i decided to make one function with a void pointer to handle all of them, rather than making one for each class

EDIT: also, forgot to say thanks, that solves it =)
jpetrie
jpetrie
Quote:

2- this is not the whole function, it's not only taking hxCamera, it will deal with a few more classes, so i decided to make one function with a void pointer to handle all of them, rather than making one for each class

There is still almost certainly a better, safer way -- you've already seen one of the bugs that void pointers can introduce, and there are plenty more. Since you're interested in learning, why don't you provide a more complete description of your problem and code and we can illustrate some better approaches.
Evil Steve
Evil Steve
Quote:
Original post by Hassanbasil
2- this is not the whole function, it's not only taking hxCamera, it will deal with a few more classes, so i decided to make one function with a void pointer to handle all of them, rather than making one for each class
Then you'd be better making a base class which contains the common functionality you need.
SirLuthor
SirLuthor
With regards to your reasons; For the first, practicing poor design is really no practice at all ;] Secondly, if the function deals with several classes, consider providing a base class which defines a common interface shared by the classes in question, and then take advantage of polymorphism, another handy language feature :] If you find that the classes in question do not in fact share functionality/interface/purpose, then they really have no business all being processed through the same function in the first place! Mind sharing a bit more of your design so that one can provide more helpful and specific advice?
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Hassanbasil
Hassanbasil
are void pointers that "bad" that everyone is suggesting not using them?

i repeat, im just practicing usage of the void pointers as im trying to learn some more about pointers ( which im actually weak at ), there is no organization problem or anything, im not even trying to do something "worthy" with that function, as it's not a part of my core class(es)
jpetrie
jpetrie
Quote:

are void pointers that "bad" that everyone is suggesting not using them?

In C++? Yes. There is very little that differentiates void pointers from regular pointers, and those differences are almost exclusively things that are bad about void pointers. Most of the good things about pointers you can learn by using non-void pointers just as easily.
KulSeran
KulSeran
Quote:

are void pointers that "bad" that everyone is suggesting not using them?

"void *" has no type, and no way to retrieve that type. It is left as an exercise to the user to know what goes in said void *.
As soon as your external interface has a void *, you are open to someone passing in the wrong thing.
Using them internally means that your code is harder to maintain as well. Since noone reading the code knows what is in the void * without reading through all the code. People will be wondering why you cast this one to a Camera* and that one to a Player*. And 6 months from now, you will be wondering the same thing.

Hassanbasil
Hassanbasil
i see, but knowing how they work is better than nothing i suppose

anyway, is there a way to get the size (sizeof) of the object, without having a size parameter in the function?
jpetrie
jpetrie
Quote:

i see, but knowing how they work is better than nothing i suppose

And knowing how to use superior alternatives is better than that.

Quote:

anyway, is there a way to get the size (sizeof) of the object, without having a size parameter in the function?

No. A void pointer points to type 'void' which is an uncompletable incomplete type. The size of incomplete types is unknown. Thus, void pointers cannot be dereferenced or have arithmetic performed on them. They must be cast to another pointer type.
kirby900
kirby900
If you were to cast the void pointer to a pointer of hxCamera, then you ought to be able to do the following:

hxCamera *ptr = (hxCamera*) my_dubious_void_ptr;
size_t obj_size = sizeof(*ptr);

However, to join the cautionary chorus, there certainly are better approaches than this.
Hassanbasil
Hassanbasil
Quote:
Original post by kirby900
If you were to cast the void pointer to a pointer of hxCamera, then you ought to be able to do the following:

hxCamera *ptr = (hxCamera*) my_dubious_void_ptr;
size_t obj_size = sizeof(*ptr);

However, to join the cautionary chorus, there certainly are better approaches than this.


if the function knew it's an hxCamera, i wouldnt need the size, would i?
Evil Steve
Evil Steve
Quote:
Original post by kirby900
If you were to cast the void pointer to a pointer of hxCamera, then you ought to be able to do the following:

hxCamera *ptr = (hxCamera*) my_dubious_void_ptr;
size_t obj_size = sizeof(*ptr);
So long as you know that gives you the same as sizeof(hxCamera), yes.
jpetrie
jpetrie
Quote:

if the function knew it's an hxCamera, i wouldnt need the size, would i?

See, the implication here is that you need to know the type of the parameter in order to make a decision. That means that either the function where this decision is taking place should be refactored into two different functions, because it does more than one thing. Or it means that the function should be taking a pointer or reference to a base type and operating on that polymorphically.

Checking the type of an input and operating on that with explicit, hard code (called type-switching) is generally extremely poor practice.
grekster
grekster
Quote:
Original post by Hassanbasil
if the function knew it's an hxCamera, i wouldnt need the size, would i?


Wouldn't you? Given that you haven't told us either what you want the size for, or what the function actually does, how the hell are we supposed to know?

Given that the only code you have posted does this cast anyway, its not an unreasonable assumption to make.
Quote: Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
Hassanbasil
Hassanbasil
it's pretty much obvious that i want the size to determine the type, as obviously sizeof ( hxCamera ) could have been enough if the function knew it's an hxCamera
Rattrap
Rattrap
So what happens if you have two classes the same size?
"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]
jpetrie
jpetrie
Quote:

it's pretty much obvious that i want the size to determine the type, as obviously sizeof ( hxCamera ) could have been enough if the function knew it's an hxCamera

This is not the proper way to determine the type at all. Two types can have the same size, and more to the point, can become the same size in two months from now.

If you must perform type-switching, use RTTI and dynamic_cast.

However, you are really going about this in one of the most suboptimal fashions possible. I appreciate that you want to "learn about pointers," but if that is the case you should be looking to use them correctly. This is not the correct place. There is a different way to handle this problem, and it has nothing to do with void pointers.

We can provide you with examples or problems that will allow you to learn and demonstrate your knowledge of pointers in general, and void pointers in particular, if you really like. But this problem is not the correct place to do so.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.