PureBasic Forum
https://www.purebasic.fr/english/

Array parameter in procedures
https://www.purebasic.fr/english/viewtopic.php?f=26&t=45640
Page 1 of 1

Author:  PureLeo [ Tue Mar 01, 2011 12:31 pm ]
Post subject:  Array parameter in procedures

Hi!

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

Code:
 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:
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

Author:  ABBKlaus [ Wed Mar 02, 2011 10:33 pm ]
Post subject:  Re: Array parameter in procedures

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:
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

Author:  PureLeo [ Wed Mar 02, 2011 11:44 pm ]
Post subject:  Re: Array parameter in procedures

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:

Author:  lexvictory [ Thu Mar 03, 2011 5:04 am ]
Post subject:  Re: Array parameter in procedures

@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...

Author:  ABBKlaus [ Thu Mar 03, 2011 10:41 am ]
Post subject:  Re: Array parameter in procedures

@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:
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
;

Author:  lexvictory [ Thu Mar 03, 2011 2:00 pm ]
Post subject:  Re: Array parameter in procedures

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

No TB:
Code:
PUSH   dword a_teste

With TB:
Code:
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 )

Author:  ABBKlaus [ Thu Mar 03, 2011 3:06 pm ]
Post subject:  Re: Array parameter in procedures

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.

Author:  lexvictory [ Fri Mar 04, 2011 11:33 am ]
Post subject:  Re: Array parameter in procedures

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

Author:  ABBKlaus [ Fri Mar 04, 2011 7:45 pm ]
Post subject:  Re: Array parameter in procedures

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:

Author:  lexvictory [ Sat Mar 05, 2011 1:21 pm ]
Post subject:  Re: Array parameter in procedures

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...

Author:  coder14 [ Thu Jul 14, 2011 8:16 am ]
Post subject:  Re: Array parameter in procedures

PureLeo wrote:
This procedure works fine.. but when I make a lib from it and run something like this:
Code:
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:
...
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.

Author:  lexvictory [ Sun Jul 17, 2011 10:14 am ]
Post subject:  Re: Array parameter in procedures

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

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/