Array copy in ADVPL

3

In ADVPL what's the difference:

aArray1 := {"A", "B", "C"}

aArray2 := aArray1

or

aArray2 := aClone(aArray1)
    
asked by anonymous 18.08.2016 / 22:02

2 answers

5

The first assignment only references memory, making the two arrays point to the same memory location. Then we have:

Conout(aArray1[2]) //Imprime B
aArray2[2] := "R" //Altera o segundo array faz a alteração no primeiro
Conout('aArray1[2]) //Imprime R

In case case 2 the array is copied:

aArray2 := aClone(aArray1)
Conout(aArray1[2]) //Imprime B
aArray2[2] := "R" //Altera o segundo array não faz a alteração no primeiro
Conout('aArray1[2]) //Imprime B

Obs. Whenever we use an array, especially when we clone them with the aClone, we should use the aSize function to clear the space used in memory.

aArray2 := aSize(array2,0) //Zera o uso da memoria.
    
18.08.2016 / 22:15
0

When doing a simple assignment of an array using the ': =' operator one variable will reference the memory address of the other using the aClone () function ( link ) variable will be assigned a real copy of the array.

    
18.08.2016 / 22:07