Array parameter in procedures

TailBite specific forum

Moderators: gnozal, ABBKlaus, lexvictory

User avatar
PureLeo
Enthusiast
Enthusiast
Posts: 221
Joined: Fri Jan 29, 2010 1:05 pm
Location: Brazil

Array parameter in procedures

Post by PureLeo »

Hi!

I'm just new to Tailbite...
I'm trying to make an userlib from this procedure:

Code: Select all

 ProcedureDLL QuickSort(Array a(1), g.l, d.l)
 
   If g < d
     v = a(d)
     i = g-1
     j = d
 
     Repeat
       Repeat
        i=i+1
       Until a(i) >= v
 
       ok = 0
       Repeat
        If j>0
          j=j-1
        Else
          ok=1
        EndIf
 
        If a(j) <= v
          ok=1
        EndIf
       Until ok<>0
 
       tmp.l = a(i)
       a(i) = a(j)
       a(j) = tmp
     Until j <= i
 
     t = a(j)
     a(j) = a(i)
     a(i) = a(d)
     a(d) = t
 
     QuickSort(a(), g, i-1)
     QuickSort(a(), i+1, d)
   EndIf
 
 EndProcedure
This procedure works fine.. but when I make a lib from it and run something like this:

Code: Select all

Dim teste(50)

teste(0) = 5
teste(1) = 2
teste(2) = 4
teste(3) = 3
teste(4) = 1
teste(5) = 6

QuickSort(teste(), 0,5) ;IMA

For a=0 To 5
	Debug teste(a)
Next
Then I get an invalid memory access error...

I'm also too tired, maybe I'm missing something obvious, sorry if thats the case
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Re: Array parameter in procedures

Post by ABBKlaus »

i don´t know if this ever worked before, but the TailBiten code does not receive the adress to the array instead it points to the adress of the first element :?

Here is a workaround if you need one :

Code: Select all

Dim teste(50)

teste(0) = 5
teste(1) = 2
teste(2) = 4
teste(3) = 3
teste(4) = 1
teste(5) = 6

temp=@teste()
QuickSort(@temp, 0,5) ;No IMA

For a=0 To 5
   Debug teste(a)
Next
User avatar
PureLeo
Enthusiast
Enthusiast
Posts: 221
Joined: Fri Jan 29, 2010 1:05 pm
Location: Brazil

Re: Array parameter in procedures

Post by PureLeo »

Yes. I've just read that DLL's procedures also couldn't have an Array as parameter if it would be changed by the procedure... didn't know about that :/

But that's the kind of workaround I was looking for!
Thank you! :mrgreen:
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Re: Array parameter in procedures

Post by lexvictory »

@ABB: does the TB code even have checks for Array or List, etc in it?
I'm not sure that it does... Which would mean the Desc doesnt have the Array, etc keyword for the parameter, but the datatype of the array...
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Re: Array parameter in procedures

Post by ABBKlaus »

@lexvictory your are right, the desc file is missing the array keyword.

If renamed to array does not work also !

@Fred / Freak could you investigate if this is a problem of the LibraryMaker.exe / PBCompiler.exe please ?

Code: Select all

ASM
;
1
KERNEL32
;
LIB
;
0
;
Quicksort_Test.chm
;
Quicksort, Long, Long, Long (Array a(1), g.l, d.l) ; <- TB generated : first paramter is wrong
;Quicksort, Array, Long, Long (Array a(1), g.l, d.l) ; <- Renamed : first paramter is Array !
None | StdCall | UNICODE | THREAD
;
Quicksort_Test_Init 
InitFunction | StdCall | UNICODE | THREAD
;
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Re: Array parameter in procedures

Post by lexvictory »

The difference appears to be how PB calls the procedure, which was my first thought...

No TB:

Code: Select all

PUSH   dword a_teste
With TB:

Code: Select all

PUSH   dword [a_teste]
So it would appear we need to do something to it when we detect an array is used (and probably for lists and maps! :x )
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Re: Array parameter in procedures

Post by ABBKlaus »

lexvictory wrote:So it would appear we need to do something to it when we detect an array is used
you can´t do anything with PUSH dword [a_teste], because its the content of the first element and in this example is a value of '5' / [05 00 00 00].
lexvictory wrote:and probably for lists and maps!
Lists are working fine, only maps need to be renamed in the desc to work properly.
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Re: Array parameter in procedures

Post by lexvictory »

ah of course...
Perhaps it is a PB problem, unless i made and error when testing...
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Re: Array parameter in procedures

Post by ABBKlaus »

ah i was only halfway right, the square-brackets means indirect adressing.

So the TailBiten procedure receives the adress to the first array-element which is normal for externalisation like in a dll.

But we need the pointer to the PB-Object we don´t have, and i guess fred would say this is no bug.

Its more or less a feature request :wink:
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Re: Array parameter in procedures

Post by lexvictory »

I thought a PB array was the same as a C array, just the initialisation numbering was different?
They certainly work the same with Vlc anyway...
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Array parameter in procedures

Post by coder14 »

PureLeo wrote:This procedure works fine.. but when I make a lib from it and run something like this:

Code: Select all

Dim teste(50)
...
QuickSort(teste(), 0,5)
Then I get an invalid memory access error...
Hi guys! Sorry to bump this topic up, but I've just stumbled into this same problem. ABBKlaus has a workaround which passes the address of the address of the array to the procedure in the library:

Code: Select all

...
temp=@teste()
QuickSort(@temp, 0,5)
This works, but I was wondering if there was any other solution to this issue. It's a little inept to expect the user to pass the address of the address of the array, rather than the array itself, to be filled.

Any ideas?

Thanks in advance.
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Re: Array parameter in procedures

Post by lexvictory »

if you can find an ASM solution we can put it in TB, otherwise it's doubtful..
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
Post Reply