Page 1 of 2
PB5.20 bug: 'Swap' does not swap pointers
Posted: Sat Oct 05, 2013 10:20 am
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!

Re: PB5.20 bug: 'Swap' does not swap pointers
Posted: Sat Oct 05, 2013 10:35 am
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.
Re: PB5.20 bug: 'Swap' does not swap pointers
Posted: Sat Oct 05, 2013 10:39 am
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!

Re: PB5.20 bug: 'Swap' does not swap pointers
Posted: Sat Oct 05, 2013 10:42 am
by STARGÅTE
What should do this code?
You can't swap the address of the array in this way!
Re: PB5.20 bug: 'Swap' does not swap pointers
Posted: Sat Oct 05, 2013 10:43 am
by Little John
Psychophanta wrote:Else, I am right and there is a critical syntax bug.
Sorry!

So where is the bug

Re: PB5.20 bug: 'Swap' does not swap pointers
Posted: Sat Oct 05, 2013 10:49 am
by Psychophanta
STARGÅTE wrote:What should do this code?
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.
Re: PB5.20 bug: 'Swap' does not swap pointers
Posted: Sat Oct 05, 2013 11:11 am
by Psychophanta
STARGÅTE wrote:What should do this code?
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

Re: PB5.20 bug: 'Swap' does not swap pointers
Posted: Sat Oct 05, 2013 11:18 am
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.
Re: PB5.20 bug: 'Swap' does not swap pointers
Posted: Sat Oct 05, 2013 1:28 pm
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.
Re: PB5.20 bug: 'Swap' does not swap pointers
Posted: Sat Oct 05, 2013 1:45 pm
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.
Re: PB5.20 bug: 'Swap' does not swap pointers
Posted: Sat Oct 05, 2013 1:54 pm
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.
Re: PB5.20 bug: 'Swap' does not swap pointers
Posted: Sat Oct 05, 2013 2:11 pm
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

Re: PB5.20 bug: 'Swap' does not swap pointers
Posted: Sat Oct 05, 2013 2:17 pm
by Danilo
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.
Re: PB5.20 bug: 'Swap' does not swap pointers
Posted: Sat Oct 05, 2013 2:34 pm
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)
Re: PB5.20 bug: 'Swap' does not swap pointers
Posted: Sat Oct 05, 2013 2:43 pm
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.