Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing Arrays
#1
I have an existing sub of the form:

Code:
declare sub myfunc (buf() as integer)

I would like to move this function out to a C library without changing any of the code apart from the declare. Am I right in assuming that this is not possible with arrays? From my testing, every other type seems to work (strings and integers can be passed ByVal, UDFs seem to be passed as pointers), but not arrays. The only current option seems to be to pass a pointer to the first element, which would mean changing all of the calling code.

Is there nothing I can do, at the point of declaration (like a ByVal), which would take an array() and turn it into something I can use in C?

If there is not, can I suggest that ByVal is implemented for arrays in a similar way to Strings? So that using:

Code:
declare sub myfunc cdecl alias "myfunc" (byval buf() as integer)

...would pass a pointer to the first element instead of just giving an error?
Reply
#2
nope, you're going to have to use
Code:
declare sub myfunc cdecl alias "myfunc" (byval buf as integer ptr)
from my understanding, at least, which is limited, to say the least.
ttp://m0n573r.afraid.org/
Quote:quote: "<+whtiger> you... you don't know which way the earth spins?" ... see... stupidity leads to reverence, reverence to shakiness, shakiness to... the dark side
...phear
Reply
#3
Using () on the argument declaration tells FB to pass the array descriptor, what C and anything but FB know nothing about.

You can declare it as BYVAL array AS array_type PTR and pass the params as @somearray(someindex) or BYREF array AS array_type, and pass it w/o the @. At C it can be array_type array[], for both.

Accessing arrays above 1 dimension can't be done as array[d1][d2] in C, as C arrays > 1D are arrays of pointers to arrays.
Reply
#4
Thanks. That's quite frustrating. It means I can't maintain code compatibility with other versions.

My final suggestion - that ByVal is implemented to give a pointer to element(0) - would be a useful enhancement.

I have decided to use some simple bridging functions so that my changes are at least contained outside the old code.

Code:
sub myfunc (buf() as integer)
   call c_myfunc (@buf(0))
end sub
Reply
#5
v1c:
i have a sort of problem then. some of the wx-c headers require arrays of strings, which must be sent as byte ptrs. so that is, in effect, a 2-dimensional array, unless you can think of some way around it?
ttp://m0n573r.afraid.org/
Quote:quote: "<+whtiger> you... you don't know which way the earth spins?" ... see... stupidity leads to reverence, reverence to shakiness, shakiness to... the dark side
...phear
Reply
#6
Code:
byte ptr ptr
Reply
#7
cool, thanks, it works!
Code:
dim choices(5) as byte ptr
    choices(0)=callocate(6)
    *choices(0)="This"
    choices(1)=callocate(4)
    *choices(1)="is"
    choices(2)=callocate(11)
    *choices(2)="one of my"
    choices(3)=callocate(11)
    *choices(3)="wonderful"
    choices(4)=callocate(10)
    *choices(4)="examples"
    listbox=wxListBox_ctor()
    wxListBox_Create(listbox,panel,ID_LISTBOX,wxSize_ctor(10,10),wxSize_ctor(120,70),5,@choices(0),wxLB_HSCROLL,0,0)
ttp://m0n573r.afraid.org/
Quote:quote: "<+whtiger> you... you don't know which way the earth spins?" ... see... stupidity leads to reverence, reverence to shakiness, shakiness to... the dark side
...phear
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)