# Can I do this?

This topic is 4036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is it possible, in C++, to use a void pointer as a function pointer? In other words, given a void pointer, can I jump to that address in memory and continue execution? Obviously this would be extremely dangerous; I'm just wondering if it's possible.

##### Share on other sites
With appropriate casting I believe so. I think this is how GetProcAddress(), dlsym() etc work.

##### Share on other sites
Oops, I didn't specify the problem completely. I should have added "without knowing the function signature at compile time".

##### Share on other sites
You must, of course, cast the pointer to the appropriate pointer-to-function type before calling it. Furthermore, you cannot do this with pointers-to-member-functions.

##### Share on other sites
Quote:

If you wanted to be clever(/evil) about it, and didn't mind getting your hands dirty with a little assembly, you could replace the stack pointer with the given address. There are a few registers that you'll need to update besides just the stack pointer, though (exactly which ones will vary, but the instruction pointer will need to be changed, for one).

Bruce Dawson discussed such a system for implementing micro-threads in Game Programming Gems 2 (read it here).

##### Share on other sites
Strictly speaking, you don't need to cast the pointer to the correct type. You can cast it to whatever you feel like and call away. Chances are your app will crash very soon after that call and you'll be left looking at some disassembly trying to work out what the correct signature is. Hey, if all else fails you could (in x86) just do something like

	void* p_myfuncptr;	__asm	{		call [p_myfuncptr]	}

Good luck with that! ;-)

##### Share on other sites
Quote:
 Original post by King of MenOops, I didn't specify the problem completely. I should have added "without knowing the function signature at compile time".

Do you know the "set of possible signatures" at compile time? And can you figure out the one to use at run-time prior to invocation?

The ability to generically invoke nearly any function dynamically is done all the time in frameworks / libraries. But just like and factory / serialization or downcasting interface scenerio ... something has to exist to manage the decisions and some data must be provided to drive the choice of which to use.

For instance you can have an abstract class like InvokableMethodType that you derive from for each signature you want to support. Each derived class can register their existence with the base class, preferably including information like an ID / GUID and some friendlier name. Then something like your game scripting language could do:

Invoke(FunctionVoidIntInt,PlotPixel,1.0,2.0)

which your engine could process as ... A) look up the invocation class "FunctionVoidIntInt" in the type / signature manager. Then lookup up the method address for PlotPixel stored probably in another manager. Then invoke it with the 2 arguments given (after casting to type).

Of course this system can be as simply or complex as needed.

##### Share on other sites
Quote:
Original post by Xai
Quote:
 Original post by King of MenOops, I didn't specify the problem completely. I should have added "without knowing the function signature at compile time".

Do you know the "set of possible signatures" at compile time? And can you figure out the one to use at run-time prior to invocation?

Well, yes, but I was kind of hoping to not have to do a lot of management work... Oh well. Casting it is, then. Although dropping into assembly would be nice if I thought I could make it work.

##### Share on other sites
Dropping to assembly would not solve the problem of having to know the types and signatures involved, really. In fact (having done it) it's probably going to result in more management.

##### Share on other sites
Ok. Still hoping for ways to save myself typing. I am trying to make a template class which takes some class T, and a function pointer returning T and taking a char* as argument; like so:

template <class T, T (*initFunction) (char*) > struct FunctorWithInitialiser {  FunctorWithInitialiser<class T, T (*initFunction) (char*) (char* str) {    value = new T((*initFunction)(str));  }private:  T* value;};

Unfortunately gcc tells me I have a syntax error before the * token, which apparently refers to the 'char*'. If I remove the char so the code reads I get a different error.

template <class T, T (*initFunction) () > struct FunctorWithInitialiser {  FunctorWithInitialiser<class T, T (*initFunction) (char*) () {    value = new T((*initFunction)());  }private:  T* value;};

FunctorWithInitialiser<T, (T)((*in))()> () specified as declarator-id
warning: no return statement in function returning non-void

So it looks as though it thinks I'm declaring a member function instead of a constructor. Is there some special syntax for template constructors that I'm not aware of? And what is the objection to the char*? I tried putting a 'typename' in front, but then I get a syntax error before 'char'. These be deep waters, matey...

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 9
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633331
• Total Posts
3011393
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!