bug?? swap 520lts (solved)

Just starting out? Need help? Post your questions and find answers here.
User avatar
kernadec
Enthusiast
Enthusiast
Posts: 146
Joined: Tue Jan 05, 2010 10:35 am

bug?? swap 520lts (solved)

Post by kernadec »

hello

swap function works more
test system (x86) windows xp sp3

best regard

Code: Select all

Dim tableau1(5): Dim tableau2(5)

For i=0 To 5
  tableau1(i)=i
  tableau2(i)=i+10
Next i
;Swap tableau1(),tableau2()        ; works to  PB500 
;Swap tableau1(),tableau2()        ; no works to  PB520 
Swap tableau1(5),tableau2(5)      ; PB 520LTS Swaps not the content of the tables

For i=0 To 5
  Debug tableau1(i)
  Debug tableau2(i)
Next i

;text in the doc  ---> Swap Tableau1() , Tableau2()    ; Swaps the contents of the tables
Last edited by kernadec on Sat Sep 21, 2013 9:19 am, edited 2 times in total.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: bug?? swap 520lts

Post by netmaestro »

Your uncommented line is swapping correctly here, PB 5.20LTS x86. Your commented line is not documented to work, as only array elements are allowed, not the whole array:
PB doc for Swap command wrote:The both <expression> have to be a variable, array, linked-list or a map element
;text in the doc ---> Swap Tableau1() , Tableau2() ; Swaps the contents of the tables
I can't find that syntax anywhere in the doc for 5.20...
BERESHEIT
User avatar
kernadec
Enthusiast
Enthusiast
Posts: 146
Joined: Tue Jan 05, 2010 10:35 am

Re: bug?? swap 520lts

Post by kernadec »

thank you netmaestro

the French doc seems to say that the entire table is swapped
if I understand I have to write like this

Code: Select all

Dim tableau1(5): Dim tableau2(5) 
	 
	For i=0 To 5 
	tableau1(i)=i 
	tableau2(i)=i+10 
	Next i 
	;Swap tableau1(),tableau2() ; works to PB500  
	;Swap tableau1(),tableau2() ; no works to PB520  

	;Swap tableau1(5),tableau2(5)      ; PB 520LTS Swaps not the content of the tables 

	For i=0 To 5 
    Swap tableau1(i),tableau2(i)      ; PB 520LTS Swaps ok
	Debug tableau1(i) 
	Debug tableau2(i) 
	Next i 
	 
	;text in the doc ---> Swap Tableau1() , Tableau2() ; Swaps the contents of the tables 
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: bug?? swap 520lts

Post by Demivec »

kernadec wrote:the French doc seems to say that the entire table is swapped
if I understand I have to write like this
It was written in the help file before version 5.10 and would allow you to swap arrays that were the same size (and only the same size) using that syntax.

Fred changed this because he says it wasn't supported and was never intended to function that way (even though the example found its way into the Help file).

Now it no longer works for arrays in any situation and the example has been removed from the help file.
User avatar
kernadec
Enthusiast
Enthusiast
Posts: 146
Joined: Tue Jan 05, 2010 10:35 am

Re: bug?? swap 520lts (résolu)

Post by kernadec »

thank you Demivec
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

PB5.20 bug: 'Swap' does not swaps pointers

Post by Psychophanta »

In that tip you are internally swapping pointers but in the tip there is not used the pointers in a syntactic way for PB, but just using the reference of the base of the array.

However, there is a bug which is critical as i see in the 5.20 version:
When there is swapped two pointers (native vartype is integer '.i' ) in a syntaxically way, i.e. using '@' or '*' as prefix, it should work.
... but it does not.
Next tip display (under PB5.20):
'Swap' only works with 2 elements of the same native type, (not structured)
But the type is native as it is just pointers.

Code: Select all

Dim tableau1(5):Dim tableau2(5)

For i=0 To 5
  tableau1(i)=i
  tableau2(i)=i+10
Next i

Swap @tableau1(),@tableau2() ; <- no works to  PB520 !!

For i=0 To 5
  Debug tableau1(i)
  Debug tableau2(i)
Next i
Sorry to say that it is dissapointing to me and to others when a newer PB version introduces some bug that didn't exist in before versions. Thanks! :|

@kernadec:
You can use this functional alternative for x86 32bit (if need it for 64bit just modify the macro accordingly or ask me for it):

Code: Select all

Macro mySwap(a,b); 32bit x86
  !mov eax,dword[a_#a]
  !xchg eax,dword[a_#b]
  !mov dword[a_#a],eax
EndMacro

Dim tableau1(5):Dim tableau2(5)
For i=0 To 5
  tableau1(i)=i
  tableau2(i)=i+10
Next i

; Swap tableau1(),tableau2()
mySwap(tableau1,tableau2)
For i=0 To 5
  Debug tableau1(i)
  Debug tableau2(i)
Next i
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
kernadec
Enthusiast
Enthusiast
Posts: 146
Joined: Tue Jan 05, 2010 10:35 am

Re: bug?? swap 520lts (solved)

Post by kernadec »

coool!!! thank you Psychophanta

I have not tried more than that
but with this macro is ok for me.

best regard
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: bug?? swap 520lts (solved)

Post by Psychophanta »

Thanks, welcome!
You can also use this one, which is slighly slower:

Code: Select all

Macro mySwap(a,b); 32bit x86
  !pushd [a_#a] [a_#b]
  !popd [a_#a] [a_#b]
EndMacro
:wink:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: bug?? swap 520lts (solved)

Post by Demivec »

@kernadec: Here is link to a routine written by Thorium that will swap memory contents. It may be useful to you.

After renaming the procedure from MemorySwapASM3() to SwapMemory() it would be used in a statement like so:

Code: Select all

SwapMemory(@tableau1(0), @tableau2(0), (ArraySize(tableu1()) + 1) * SizeOf(Integer))  ;assumes tableu1() and tableau2() are the same size
User avatar
kernadec
Enthusiast
Enthusiast
Posts: 146
Joined: Tue Jan 05, 2010 10:35 am

Re: bug?? swap 520lts (solved)

Post by kernadec »

thank you very much for this interesting link Demivec.
I put all that aside.
thank you Thorium for your work.

best regard
Post Reply