#include <array.au3> Global $avOne[2] = [1, 2] Global $avTwo = $avOne ;~ $avTwo[0] = $avOne[0] ; breaks the link so the weird behaviour is not seen. _Swap($avTwo[0], $avTwo[1]) _ArrayDisplay($avOne, "Debug: $avOne") _ArrayDisplay($avTwo, "Debug: $avTwo") Func _Swap(ByRef $vA, ByRef $vB) Local $T = $vA $vA = $vB $vB = $T EndFunc ;==>Swap
Problem with array assignment and ByRef functions
#1
Posted 03 January 2008 - 05:55 AM
#2
Posted 03 January 2008 - 10:51 AM
In the third line you're assiging $avTwo to point to the same memory address as $avOne, so they're both pointing to the same thing. When you swap $avTwo, only $avTwo is swapped, but both arrays are still pointing to the same thing so effectively they're both swapped. If you change the line to Global $avTwo[2] = [1,2] or Global $avTwo[2]Using release 3.2.10.0, I have found a very disconcerting issue. I was creating a set of sorting functions in AutoIt when I noticed a strange behaviour of array assignment. When I assign a variable from an entire array and pass the variable to a UDF ByRef, I find that the original array is affected as well. Here is an example that one of the other users in Genaral Help simplified for me and it has the effect of modifying $avOne by playing with $avTwo. After the assignment, shouldn't these be separate arrays? The only work-around I have found so far is to assign to the element of $avTwo, which seems to break the link and actually have the arrays be copies.
#include <array.au3> Global $avOne[2] = [1, 2] Global $avTwo = $avOne ;~ $avTwo[0] = $avOne[0] ; breaks the link so the weird behaviour is not seen. _Swap($avTwo[0], $avTwo[1]) _ArrayDisplay($avOne, "Debug: $avOne") _ArrayDisplay($avTwo, "Debug: $avTwo") Func _Swap(ByRef $vA, ByRef $vB) Local $T = $vA $vA = $vB $vB = $T EndFunc ;==>Swap
$avTwo[0] = $avOne[0]
$avTwo[1] = $avOne[1], only $avTwo is changed by the call to _Swap.
Edited by JerryD, 03 January 2008 - 10:53 AM.
#3
Posted 03 January 2008 - 01:10 PM
In my opinion the behaviour is not correct. You don't want $avOne to be swapped at all. So you "copy" the array to new array and only work with the new array. Don't think the old array should change at same time.In the third line you're assiging $avTwo to point to the same memory address as $avOne, so they're both pointing to the same thing. When you swap $avTwo, only $avTwo is swapped, but both arrays are still pointing to the same thing so effectively they're both swapped. If you change the line to Global $avTwo[2] = [1,2] or Global $avTwo[2]
$avTwo[0] = $avOne[0]
$avTwo[1] = $avOne[1], only $avTwo is changed by the call to _Swap.
#4
Posted 03 January 2008 - 01:37 PM
#5
Posted 03 January 2008 - 02:36 PM
You're right, so don't do it! When you use ByRef in a function, what you're saying is "change the value of the variable contained in the memory address REFERENCED by the variable passed. In other languages (in C anyway) pointers are not handled as elegantly or easily as in AutoIt, but that's all ByRef means - use the value at the memory address POINTED TO by this variable - not the VALUE of the variable.Variants should never be a reference to another variable unless passed ByRef. I believe this is incorrect behavior.
When you create the variable Global $avOne[2], you're saying that the variable $avOne is a MEMORY ADDRESS that points to two variables $avOne[0] and $avOne[1], each of which contain values.
When you declare $avTwo and assign it to the value of $avOne, the only value it can be assigned is the value of $avOne which is a memory address.
While I understand your confusion, but most languages would behave the same, and more importantly, once you know about it, you know how to use it properly.
#6
Posted 03 January 2008 - 03:13 PM
This is a bug.
#7
Posted 03 January 2008 - 03:23 PM
Hate to tell you Valik, but I actually DO have a clue, and have been working with programing and computers longer than you've been working on being a nasty snotty SOB.Ummm Jerry, you might want to shut up now. You have no clue what you're on about. The line "Global $avTwo = $avOne" is a COPY. It is not the initialization of a pointer. AutoIt doesn't use pointers to Variants. You've just fabricated a whole story why this works which makes no sense in AutoIt.
This is a bug.
I don't mind being wrong, but the fact is my explanation makes perfect sense and is not off base, especially for someone that has no knowledge of the inner workings of the language, hence the reason for my post.
So what's the reason for yours? Just enjoy being an ahole?
#8
Posted 03 January 2008 - 04:03 PM
Good to hear that it's getting fixed; it was becoming a teeny bit annoying to test some functions because I kept having to remember to write to the array once before testing
Edited by -Ultima-, 03 January 2008 - 04:13 PM.
#9
Posted 05 January 2008 - 04:31 PM
Need Jon or Valik to analyze
#10
Posted 09 January 2008 - 03:18 PM
Fixed 3.2.11.0
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users





