Get Function Pointer as an Int?

BlitzMax Forums/BlitzMax Beginners Area/Get Function Pointer as an Int?

Gabriel(Posted 2006) [#1]
I can cast a VarPtr to an int, but it doesn't seemt to want to let me do it with a function pointer. So is there a way to get the address of a function as a 32bit integer?


REDi(Posted 2006) [#2]
Function test()
EndFunction

Print Int(Byte Ptr(test))



Gabriel(Posted 2006) [#3]
Doh, I knew I had to be missing something obvious. Thanks.


ImaginaryHuman(Posted 2006) [#4]
Yah you can only change function pointers into byte pointers and vice versa. Even if you want an Int Ptr you have to make it into a Byte Ptr first.


gman(Posted 2006) [#5]
another nice thing about Byte Ptrs is that they are like VOID* in C. they can be cast to any pointer type:
SuperStrict
Framework BRL.Basic

Local test:Byte Ptr

Local inttest:Int=10
test=Varptr(inttest)
DebugLog(inttest+" | "+Int Ptr(test)[0])

Local floattest:Float=9.888
test=Varptr(floattest)
DebugLog(floattest+" | "+Float Ptr(test)[0])

Local strtest:String="yup"
test=Varptr(strtest)
DebugLog(strtest+" | "+String Ptr(test)[0])

Local typetest:pointertest=New pointertest
test=Varptr(typetest)
DebugLog(typetest.mystring+" | "+pointertest Ptr(test)[0].mystring)

DebugLog("etc, etc, etc...")

Type pointertest
	Field mystring:String="field of pointertest"
EndType



Eric(Posted 2006) [#6]
May I ask what does having the pointer to a function do for us programmers?? I mean what can be done with them, that is not practical in other means.


gman(Posted 2006) [#7]
probably the biggest thing for function pointers is callbacks from C/C++ routines. by setting a function pointer in C, the C code can call your BlitzMax function. i use this extensively to allow things like custom scene nodes in the Irrlicht mods.


ImaginaryHuman(Posted 2006) [#8]
It also lets you customize an otherwise `hardwired` sequence of function calls at runtime, which wouldn't normally be possible, or would require a bunch of if's or cases. It lets you re-wire your application after it's been compiled, to be more efficient. There are some other uses as well I'm sure.


Eric(Posted 2006) [#9]
Any simple examples?


ImaginaryHuman(Posted 2006) [#10]
Local myfunctionptr()

Function hello()
   Print "Hello"
End Function

Function hello2()
   Print "Hello 2"
End Function

myfunctionptr=hello
myfunctionptr() ' Prints Hello
myfunctionptr=hello2
myfunctionptr()  ' Prints Hello 2

In other words you can decide what function is called at runtime instead of at compile-time. You could have one single generic framework of a routine, which calls one function by way of a function pointer, then before you enter the routine or elsewhere you could `rewire` which function is called by just making your function call point to some other function. Saves you having to re-write the code for lots of different specific situations. Cuts down on code repetition. Also used constructively can make your software much more flexible at runtime than it could otherwise be.