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

Just starting out? Need help? Post your questions and find answers here.
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 swap pointers

Post by Psychophanta »

There is a bug which could be 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 displays (under PB5.20):
'Swap' only works with 2 elements of the same native type, (not structured)
But the used types are 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 previous versions. Thanks! :|
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: PB5.20 bug: 'Swap' does not swap pointers

Post by Little John »

Psychophanta wrote:

Code: Select all

Swap @tableau1(),@tableau2() ; <- no works to  PB520 !!
Documentation of Swap wrote:Swaps the value of the both expression, in an optimized way. The both <expression> have to be a variable, array, linked-list or a map element
In your code, you are trying to swap 2 addresses. This is not supported. So no bug.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: PB5.20 bug: 'Swap' does not swap pointers

Post by Psychophanta »

Little John wrote:
Psychophanta wrote:

Code: Select all

Swap @tableau1(),@tableau2() ; <- no works to  PB520 !!
Documentation of Swap wrote:Swaps the value of the both expression, in an optimized way. The both <expression> have to be a variable, array, linked-list or a map element
In your code, you are trying to swap 2 addresses. This is not supported. So no bug.
Exactly, and you are right, there is no bug while addresses variables are not native variables types,
Else, I am right and there is a critical syntax bug.
Sorry! :)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: PB5.20 bug: 'Swap' does not swap pointers

Post by STARGÅTE »

What should do this code?

Code: Select all

Swap @tableau1(),@tableau2()
You can't swap the address of the array in this way!
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: PB5.20 bug: 'Swap' does not swap pointers

Post by Little John »

Psychophanta wrote:Else, I am right and there is a critical syntax bug.
Sorry! :)
So where is the bug :?:
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: PB5.20 bug: 'Swap' does not swap pointers

Post by Psychophanta »

STARGÅTE wrote:What should do this code?

Code: Select all

Swap @tableau1(),@tableau2()
You can't swap the address of the array in this way!
What should do?
Just try it in PB5.00. That should do: to swap the value of 2 variable pointers.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: PB5.20 bug: 'Swap' does not swap pointers

Post by Psychophanta »

STARGÅTE wrote:What should do this code?

Code: Select all

Swap @tableau1(),@tableau2()
It should do this (it is for 32bit x86 for 64bit x86 just please modify code accordingly):

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 !!
!mov eax,dword[a_tableau1]
!xchg eax,dword[a_tableau2]
!mov dword[a_tableau1],eax
For i=0 To 5
  Debug tableau1(i)
  Debug tableau2(i)
Next i
Another example about what shout do 'Swap':

Code: Select all

v1.i=45
v2.i=555
Debug v1
Debug v2

;What sould do 'Swap @v1,@v2' :
!mov eax,dword[v_v1]
!xchg eax,dword[v_v2]
!mov dword[v_v1],eax
Debug v1
Debug v2
:o
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: PB5.20 bug: 'Swap' does not swap pointers

Post by Psychophanta »

What Fred will say is that '@' prefixed pointers (i.e. variables finally) can not be written with new values, but just the '*' prefixed ones. Just becouse it is ugly and because it can bring up problems in the codes.
The answer for the ugly is that uglyness is subjective,
and the answer for the problems is that the responsability of the code falls down to the programmer.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: PB5.20 bug: 'Swap' does not swap pointers

Post by Danilo »

Psychophanta wrote:What Fred will say is that '@' prefixed pointers (i.e. variables finally) can not be written with new values, but just the '*' prefixed ones.
Yes, '@' is an operator to get the address of something (variables, procedures). The '@' operator is read-only,
so '@var = 12' is not supported. It does not make sense to change the address of an 'object' with the '@' operator.
.
'*' is used for readable and writeable pointer variables. To get the address of the pointer, you use the '@' operator, for example 'x = @*pointer',
and again, the address of the variable '*pointer' is read-only. You can't change the address of the variable '*pointer' in memory.
It is just the pointer variable itself that is readable and writeable.

Swap works with pointer variables:

Code: Select all

Define *pointer1 = 1, *pointer2 = 2

Debug *pointer1
Debug *pointer2

Swap *pointer1, *pointer2

Debug *pointer1
Debug *pointer2
Swapping the addresses of the pointer variables would not make sense:

Code: Select all

Define *pointer1 = 1, *pointer2 = 2

Debug @*pointer1
Debug @*pointer2

Swap @*pointer1, @*pointer2

Debug @*pointer1
Debug @*pointer2

Pointers and the '@'-address-operator are not the same.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: PB5.20 bug: 'Swap' does not swap pointers

Post by Psychophanta »

Danilo wrote:The '@' operator is read-only
Swapping the addresses of the pointer variables would not make sense
Why?

I know the answer:
It is a feature, not a bug.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: PB5.20 bug: 'Swap' does not swap pointers

Post by Danilo »

Psychophanta wrote:
Danilo wrote:The '@' operator is read-only
Swapping the addresses of the pointer variables would not make sense
Why?

I know the answer:
It is a feature, not a bug.
A variable is just the human readable form of an memory address, and this memory address is fixed at runtime.
You can't change the address of the memory address 5000. It will always be memory address 5000.

You can change the content of memory at address 5000, though. It is still memory address 5000 then, just different content.

Code: Select all

Define *pointer1 ; define variable *pointer1, means: get a free memory address and assign the alias '*pointer' to it, as its human readable name

*pointer = 123   ; write to the memory address (the memory address alias)

x = *pointer     ; read from the memory address (the memory address alias)

Debug @*pointer  ; read the memory address of the variable (the memory address alias)

@*pointer = 12   ; change the memory address of the memory address - not possible
You can't change the memory address of the variable *pointer, because '*pointer' is just an alias for a memory address.
Changing the memory address of a memory address does not make sense.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: PB5.20 bug: 'Swap' does not swap pointers

Post by Psychophanta »

Danilo wrote:
Psychophanta wrote:
Danilo wrote:The '@' operator is read-only
Swapping the addresses of the pointer variables would not make sense
Why?

I know the answer:
It is a feature, not a bug.
A variable is just the human readable form of an memory address, and this memory address is fixed at runtime.
You can't change the address of the memory address 5000. It will always be memory address 5000.

You can change the content of memory at address 5000, though. It is still memory address 5000 then, just different content.
Sorry Danilo, but this argument is not valid here: in fact we can change the value of the mem-address (base) of an array:
http://www.purebasic.fr/english/viewtop ... 63#p427313

So why to go to ASM? Just because PB developers say it is a feature, and not a bug :o
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: PB5.20 bug: 'Swap' does not swap pointers

Post by Danilo »

Psychophanta wrote:Sorry Danilo, but this argument is not valid here: in fact we can change the value of the mem-address (base) of an array:
http://www.purebasic.fr/english/viewtop ... 63#p427313
So an array itself is represented with a pointer that points to memory that actually contains the array data (the content).
You swap this pointers, but you don't swap the addresses of 'a_a' and 'a_b', because this variables are at fixed addresses at runtime.

Code: Select all

Global address_of_a_a, address_of_a_b

Macro mySwap(a,b); 32bit x86
    Debug "address of a_a"
    !mov dword [v_address_of_a_a], a_#a
    Debug address_of_a_a
    
    Debug "address of a_b"
    !mov dword [v_address_of_a_b], a_#b
    Debug address_of_a_b
    
    Debug "Swaping pointer content..."
    !mov eax,dword[a_#a]
    !xchg eax,dword[a_#b]
    !mov dword[a_#a],eax
  
    Debug "address of a_a"
    !mov dword [v_address_of_a_a], a_#a
    Debug address_of_a_a
    
    Debug "address of a_b"
    !mov dword [v_address_of_a_b], a_#b
    Debug address_of_a_b

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)

Debug "-----"

For i=0 To 5
  Debug tableau1(i)
  Debug tableau2(i)
Next i
The addresses of the memory aliases a_a and a_b don't change. You change the content, but you can't change the addresses.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: PB5.20 bug: 'Swap' does not swap pointers

Post by Demivec »

Psychophanta wrote:There is a bug which could be 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 displays (under PB5.20):
'Swap' only works with 2 elements of the same native type, (not structured)
But the used types are native as it is just pointers.
Not a bug. You've answered your own question. It is not syntactically correct to use the '@' operator this way.

Psychophanta wrote:

Code: Select all

Swap @tableau1(),@tableau2() ; <- no works to  PB520 !!
This would be the equivalent of trying to execute this statement which is also incorrect and will likewise fail:

Code: Select all

Swap 15, 4000 ;these are values not native variables (see the help file)
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: PB5.20 bug: 'Swap' does not swap pointers

Post by Danilo »

Just give yourself some time, Psychophanta. ;)

The PureBasic '@'-operator is the same as the '&'-operator in C.
I was also confused at the beginning when learning C, because
I thought it is the same as pointers. But is is very different -
it is an read-only address operator, not a pointer.
Post Reply