FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Array BUG?
Posts: 1048
Joined: Mon Oct 24, 2005 09:54 AM
Array BUG?
Posted: Thu Feb 21, 2013 03:25 PM
I make a array with arr := array(30) and then aFill(arr,{}).
When I now say aadd(arr[1],{1,2,3,4}]) all arrays in arr become this aadd() not only arr[1]!
Whe i fill the arr with aeval(arr,{|,index|aadd(arr[index],{1,2,3,4}}) then is it ok.
I use xHarbour.
Regards,
Günther
---------------------------------
office@byte-one.com
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Array BUG?
Posted: Thu Feb 21, 2013 03:45 PM

It is not a bug. It is the expected behavior.

AFill( aArray, {} ) results in filling all elements of aArray with reference to the pointer of the same array {}

Let us see this example:

x := y := {} // both x and y contain the pointer to the same array.
So whether we use the variable x or y, we are operating on the same array.

AAdd( x, 1 ) // this is same as AAdd( y, 1 ) because x and y hold reference to the same array.
Now x[ 1 ] contains 1 and y[ 1 ] also contains 1

AFill( aArray, Array(0) ) is the recommended way. Each element is assigned with pointer to a different array.

Regards



G. N. Rao.

Hyderabad, India
Posts: 1048
Joined: Mon Oct 24, 2005 09:54 AM
Re: Array BUG?
Posted: Thu Feb 21, 2013 06:09 PM

Thanks much! Is now clear.

Regards,
Günther
---------------------------------
office@byte-one.com

Continue the discussion