Two-dimensional array

Hi,
I have a large global 2d array and a number of small 2d arrays on function level. I write integers to the small arrays and then store the small arrays in the large array.
Problem: When I read back the small arrays from the large array, all small arrays had become identical to the last written small array, like this (simplified):
// Write
put (arrSmall, 10, 0, 0)
put (arrLarge, arrSmall, 0, 0)
put (arrSmall, 20, 0, 0)
put (arrLarge, arrSmall, 1, 0)
// Read
Array arrTemp = (Array get(arrLarge, 0, 0))
int var1 = (int get(arrTemp, 0, 0))
arrTemp = (Array get(arrLarge, 1, 0))
int var2 = (int get(arrTemp, 0, 0))

I expect var1 == 10 and var2 == 20, but when I print var1 and var2, both of them == 20.

I'm using DOORS 8.3. Every comment is very much appreciated.
Karl-Erik - Wed Mar 10 01:51:19 EST 2010

Re: Two-dimensional array
Mathias Mamsch - Wed Mar 10 02:31:29 EST 2010

You need to understand that arrays are objects which like other objects in DXL are always passed by reference. This means if you do

myArr = arrSmall

then you will make a reference to arrSmall not a copy of it. The same happens when you pass the array to functions. In your code:
 

put (arrSmall, 10, 0, 0)
// Here you put a REFERENCE to the arrSmall in the large array, not a copy
put (arrLarge, arrSmall, 0, 0)
// Here you are changing the 0,0 entry of the reference 
// Your are missing a line like: arrSmall = create(1,1)
put (arrSmall, 20, 0, 0)
put (arrLarge, arrSmall, 1, 0)

 


If you really want to put copies in your arrLarge, then you would have to create a new arrSmall array and copy the entries in there manually.

Regards, Mathias

 

Re: Two-dimensional array
Karl-Erik - Wed Mar 10 08:30:46 EST 2010

Mathias Mamsch - Wed Mar 10 02:31:29 EST 2010

You need to understand that arrays are objects which like other objects in DXL are always passed by reference. This means if you do

myArr = arrSmall

then you will make a reference to arrSmall not a copy of it. The same happens when you pass the array to functions. In your code:
 

put (arrSmall, 10, 0, 0)
// Here you put a REFERENCE to the arrSmall in the large array, not a copy
put (arrLarge, arrSmall, 0, 0)
// Here you are changing the 0,0 entry of the reference 
// Your are missing a line like: arrSmall = create(1,1)
put (arrSmall, 20, 0, 0)
put (arrLarge, arrSmall, 1, 0)

 


If you really want to put copies in your arrLarge, then you would have to create a new arrSmall array and copy the entries in there manually.

Regards, Mathias

 

Thanks, Karl-Erik

Re: Two-dimensional array
llandale - Wed Mar 10 10:50:04 EST 2010

Mathias Mamsch - Wed Mar 10 02:31:29 EST 2010

You need to understand that arrays are objects which like other objects in DXL are always passed by reference. This means if you do

myArr = arrSmall

then you will make a reference to arrSmall not a copy of it. The same happens when you pass the array to functions. In your code:
 

put (arrSmall, 10, 0, 0)
// Here you put a REFERENCE to the arrSmall in the large array, not a copy
put (arrLarge, arrSmall, 0, 0)
// Here you are changing the 0,0 entry of the reference 
// Your are missing a line like: arrSmall = create(1,1)
put (arrSmall, 20, 0, 0)
put (arrLarge, arrSmall, 1, 0)

 


If you really want to put copies in your arrLarge, then you would have to create a new arrSmall array and copy the entries in there manually.

Regards, Mathias

 

I know this exists for History as well; you cannot plow through History looking for the most recent 'modify' by assigning it to a holding variable. Skip lists are like this as well. But not Links, Objects, nor Modules, nor DxlObjects.

What other variable types means 'alias' when you do this:
Variable1 = Variable2?

I suspect, perhaps, its those in the manual that do not explicitely say "assignment: a=b".

  • Louie