MudBytes
Pages: << prev 1 next >>
void *pointers
JohnnyStarr
Wizard






Group: Members
Posts: 822
Joined: Feb 14, 2009

Go to the bottom of the page Go to the top of the page
#1 id:43250 Posted Mar 10, 2010, 2:18 pm

Because I need some sort of polymorphism when hooking into Lua, I have considered using void* pointers
as parameters to my call_lua(...) function. From what I've read this can be an inefficient use of memory.
Is this true? If so, can you explain why?

Because each call to lua may require 1 or more objects, would it just be better to construct a container
of pointers to these objects, then send in the head pointer to this container? Just an idea anyway.
.........................

"It's not the daily increase but daily decrease. Hack away at the unessential." - Bruce Lee

Kline
Wizard




Group: Members
Posts: 587
Joined: Dec 14, 2007

Go to the bottom of the page Go to the top of the page
#2 id:43255 Posted Mar 10, 2010, 2:27 pm

Last time I ever tried to do something where I figured void pointers were easiest, I had a lot of things yelling at me for casting things back and forth. That, and someone suggested a union of just the types you'll ever require, instead.
.........................
AckFUSS -- Check it out.

David Haley
Wizard






Group: Members
Posts: 6,874
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#3 id:43257 Posted Mar 10, 2010, 2:30 pm

Where did you read that it's an inefficient use of memory? It depends on what you're doing. If you're storing chars, then yes using a void pointer that points to a char is wasteful (3 or 7 bytes depending on various things). (EDIT: Sometimes, you have no choice.)

Sometimes you don't have the option of using a union, like when interfacing with Lua for example, or in general when having general user data pointers to data types that you don't know about. (This is a very common situation for callback libraries.)
Anyhow Lua doesn't know about your types in the first place, so it wouldn't understand your type union; it would only understand void*.
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Last edited Mar 10, 2010, 2:30 pm by David Haley
JohnnyStarr
Wizard






Group: Members
Posts: 822
Joined: Feb 14, 2009

Go to the bottom of the page Go to the top of the page
#4 id:43302 Posted Mar 10, 2010, 10:08 pm

David Haley said:
Where did you read that it's an inefficient use of memory?

I was googling and found this.
I had considered a union, but came to the same conclusion. I was searching my code base for some uses of void * and stumbled on the "act" routine.
Code (c++):
1
2
3
void act (const char *format, CHAR_DATA *ch, const void *arg1, const void *arg2, int type)

It's signature uses voids and works quite well. I am just trying to avoid writing hackish code, and because my knowledge of C is at an intermediate stage,
I would like to continue to learn the "right" way.
.........................

"It's not the daily increase but daily decrease. Hack away at the unessential." - Bruce Lee

Last edited Mar 10, 2010, 10:10 pm by JohnnyStarr
Runter
Wizard






Group: Members
Posts: 1,850
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#5 id:43303 Posted Mar 10, 2010, 10:35 pm

The only reason they would be considered less efficient is because you would possibly need overhead to determine what it actually points to instead of it being baked in.  This isn't necessarily true.  It may just so happen that other indicators of what the void pointer is already exist.    In any event, I wouldn't use a void pointer unless I had to.

.........................
CoralMud project

For once you have tasted flight Ruby you will walk the earth with your eyes turned skywards,
for there you have been and there you will long to return. --
                                              Leonardo Da Vinci Yukihiro Matsumoto

David Haley
Wizard






Group: Members
Posts: 6,874
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#6 id:43308 Posted Mar 11, 2010, 1:14 am

The "right" way to use void* is when you really don't know what kind of type might be coming in. A very classic example of this is callback functions where you can supply userdata to those callbacks. For example,

Code (text):
1
2
3
register_mouse_callback(my_mouse_function, &my_app_state);


The mouse handler library function "register_mouse_callback" takes as arguments two things: (1) a function and (2) user data to pass to that function. Let's take a look at how my_mouse_function might be defined:

Code (text):
1
2
3
4
5
6
7
void my_mouse_function(int x, int y, void* userdata) {
  AppState* state = (AppState*) userdata;
  state->mouseX = x;
  state->mouseY = y;
}


The event handler library really has no idea what kind of data you might find useful to pass to the mouse callback. So, it lets you pass in whatever the heck you want, on the assumption that you won't screw up in your callback and will handle the void* appropriately. In this case, we know that the callback is only called when the void* points to an AppState, so we can safely cast it.

There are other places you might use a void*, like what Runter alluded to where you have a type indicator alongside the void*. This is a common technique for implementing dynamic features where you can switch on the type indicator and do the right thing with the pointer. (This is one way to implement OOP in straight C, for example. Not necessarily the best way, but a way.)
The 'act' function does something like this. It relies on the contents of the format string to tell it what the void* is. If the format string uses $o (or whatever it is), the code knows that the void* must point to an object. (etc.)
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Pages:<< prev 1 next >>
Tags
[+]

Valid XHTML 1.1! Valid CSS!