What's wrong with this function pointer program?

BlitzMax Forums/BlitzMax Beginners Area/What's wrong with this function pointer program?

ImaginaryHuman(Posted 2004) [#1]
???

Rem

Using function pointers to change which function is called dynamically at runtime

With an bank of function pointers

End Rem

Local myfunctions:TBank=CreateBank(10*4)
Local myfunctionsbuf:Byte Ptr
myfunctionsbuf=BankBuf(myfunctions)

Function funct1(param:Int)
        Print "You're in function 1, passed counter parameter="+param
End Function

Function funct2(param:Int)
        Print "You're in function 2, passed counter parameter="+param
End Function

Local counter:Int
Local offset:Int
For counter=0 To 9 Step 2
        offset=counter Shl 2
        (Int Ptr(myfunctionsbuf+offset))[0]=Int(funct1) ' Something wrong with this line
        (Int Ptr(myfunctionsbuf+offset+4))[0]=Int(funct2)
Next

For counter=0 To 9
        offset=counter Shl 2
        (Int Ptr(myfunctionsbuf+offset))[0](counter)
Next

End



ImaginaryHuman(Posted 2004) [#2]
Anyone? I just need need to know how to write a function pointer to a bank and then access that function from the bank.


Floyd(Posted 2004) [#3]
Why try to force function pointers into a bank? I would think a bank is 'raw data'.

An array would seem more appropriate.

Function funct1(param:Int)
        Print "You're in function 1, passed counter parameter="+param
End Function

Function funct2(param:Int)
        Print "You're in function 2, passed counter parameter="+param
End Function


Local f(n)[10]  ' f[0] to f[9] are function pointers

f[1] = funct1
f[2] = funct2

f[1](100)
f[2](200)



Kanati(Posted 2004) [#4]
You are thinking too ASM-like angel... arrays. Memory blocks aren't the best choice here. :)


ImaginaryHuman(Posted 2004) [#5]
Yes yes yes, I've already been through all that. Thankyou for your opinions and making decisions for me but I'm not asking how to achieve a given result using whatever method, I need to use this method. I have an array of function pointers working fine already. I have particular reasons why I would like this to work with a bank. I don't want the extra overhead of finding out where in memory to read the array data from every time I access it, for one. With a bank I can directly access the data using a single pointer variable. This is for a very tight loop and needs to be highly optimized. I've heard all the other ways of doing what I'm trying to do. I don't need another way of doing it, I need to find out how to get THIS way to work. I also don't need to know the reasons and in's and out's of why using banks is a wrong approach, or why other approaches are better, or more elegant, or make code easier to read, or that I should optimize later when I've got a simpler version working. Skip all that. I just need to know how to get these function pointers stored in a chunk of memory other than an array or a list (ie in a Bank), and then how to most efficiently call the function from the bank. It must be possible I'm sure, I am just not sure how to get the syntax right and to cast variables properly etc.

The line that I'm using to write the function pointer into memory is mostly taken from the code used by BlitzMax in the PokeInt command. It should write a value to memory at the given buffer address plus an offset. The compiler doesn't like it. Maybe it's because I created the first part as an Int Ptr, so it expects the written value to be an int (which is why I try to cast the function pointer as an Int) .. but it doesn't accept it. Maybe BlitzMax doesn't even make it possible to do something like that with a function pointer, or maybe I need to change it into another form somehow. ??

Thanks.