Confusion with Byte Pointers and Function Pointers

BlitzMax Forums/BlitzMax Programming/Confusion with Byte Pointers and Function Pointers

ImaginaryHuman(Posted 2007) [#1]
In the below program, a function is defined and its function pointer is then stored in a byte pointer. Then two separate function pointers become equated with the byte pointer and are then called. Each of the function pointers is defined with a different number of parameters. One of them does not the match the number of parameters in the mapped function. The program compiles and runs.

It prints "hi 5" twice. When func2() is called, does the value 5 get passed into the function and printed? How is BlitzMax remembering it? Is this remembering deliberate (and reliable) or just some `left over` data? Also, when activating the line Hi(10), the value 10 then carries over to the func2() call. Why?

Since the function pointer is converted into a byte pointer, and the byte pointer has no definition of how many parameters should go with it, and is then blindly converted back to a function, how is the function pointer then working correctly if it is passing the value that the previous call passed? Does the byte pointer somehow store information about the function? Is BlitzMax keeping the local register data that was filled in when the first function passed a 5 and is now referring back to it - and it just happens that it still contains the 5 and hasn't been trashed?

If you set the parameter of the original Hi() function to Hi(a:Int=15), whereby it should fill in a 15 if no value is passed, the call to Func2() does NOT print "hi 15", it still prints whatever was the previously passed value.

What's going on here? When the function is converted from a byte pointer, does it lose track of how many parameters it has and thus is unable to activate the default =15 feature?

I'm confused.
Local myfunc(param1:Int)
Local myfunc2()
Local mybytepointer:Byte Ptr

Function hi(a:Int)
	Print "hi "+a
End Function

myfunc=hi
mybytepointer=myfunc
myfunc=mybytepointer
myfunc2=mybytepointer

myfunc(5)
'hi(10)
myfunc2()

What I'd like to be able to do is have a number of functions with different numbers of parameters and different data types for each parameter, and then store all of these functions in byte pointers. Then later I want to be able to convert the byte pointers back to functions and call the functions and pass to them the appropriate amounts of data in the appropriate type. If converting from a byte pointer to a function pointer means that it does not keep track of how many parameters are asked for or of what type, this would actually be beneficial to me since I could then just pass a bunch of Int's as parameters, even if the function is asking for things like byte pointers, pointers to custom types, etc (I would be using HandleToObject and HandleFromObject etc to convert for storage). Would this work? I'm guessing not.

This is aside from the above described behavior which doesn't entirely affect what I'm trying to do but it is a curiosity nonetheless.


Floyd(Posted 2007) [#2]
I think it works as follows. BlitzMax knows how parameters should be passed based on the declaration of the function pointer.

myfunc has one integer parameter. So a call to myfunc(5) pushes a 5 onto the stack and then jumps to the address in myfunc.
myfunc2 has no parameters so it simply jumps to the specified address.

Both fuction calls jump to address in hi. This retrieves one value from the stack and prints it. If myfunc2() prints a 5 then that just means that nothing disturbed the stack since the call to myfunc(5).


ImaginaryHuman(Posted 2007) [#3]
Hmm ok I guess that makes sense. But I guess you can't really rely on the stack not being disturbed, there could be all sorts of other things that disturb it.