We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Swapping/copying arrays
Page Index Toggle Pages: 1
Swapping/copying arrays (Read 1067 times)
Swapping/copying arrays
Nov 26th, 2009, 3:29am
 
I am writing a program where I use two arrays for storing coordinates of points in space.
The first array stores the current position of everything and the other array the future positions.  That is, I perform calculations based on the current array and store these in the future array. In the end of my draw function I then use arrayCopy (future, current) to go to the next iteration.
My problem is that when I perform a change to the future array it seems to affect the current? I print the values and can see both of them changing even though I only adress one of them. It therefore appears  that they behave as if they are the same array with two names?

The sketch is unfortunately a bit long for copying here but the process is something like this:

Declare and create arrays

In setup: Assign (identical) values to the arrays for their origin position

In draw: Loop through the arrays. Calling a function that changes the values in the future array by calculations based on the current array.

After this main loop I copy the values from future to current and so on…

Any help/hints/thoughts would be greatly appreciated. Thank you
Re: Swapping/copying arrays
Reply #1 - Nov 26th, 2009, 3:44am
 
I don't have a huge amount of experience on this, but IIRC depending on how you 'copy' your array, your 'copy' may simply act as a pointer to the original array; which sounds like what's happening in your case.

If you search the forum you'll find the problem has been addressed before.  In the meantime it would be worth posting how you 'copied' the array.

One option which should be safe - once you've confirmed you have declared two separate arrays - would be to iterate through the two arrays and 'manually' copy the raw data across; something like:

Code:
for (int i=0;i<futureArray.length; i++) {
futureArray[i] = oldArray[i];
}
Re: Swapping/copying arrays
Reply #2 - Nov 26th, 2009, 5:03am
 
thank you blindfish

Well actually that seems to work. I tried "arraycopy(futureArray, oldArray)" and just plain "oldArray=futureArray" and here the pointer thing happened. Now I just wonder why and when what happens?
Re: Swapping/copying arrays
Reply #3 - Nov 26th, 2009, 6:16am
 
jacob Riiber wrote on Nov 26th, 2009, 5:03am:
thank you blindfish

Well actually that seems to work. I tried "arraycopy(futureArray, oldArray)"


If you look at the reference for arraycopy() it looks to me like you tried it with the arguments the wrong way round:

Quote:
arrayCopy(src, dest)

If you want to copy from oldArray into futureArray that should have been:

Code:
arrayCopy(oldArray, futureArray) 


Might be worth double-checking if that works, as arraycopy() should be faster than iterating through the array...
Re: Swapping/copying arrays
Reply #4 - Nov 26th, 2009, 2:07pm
 
I suggest you use
Code:
arrayCopy(oldArray, futureArray) 

rather than
Code:
arraycopy(oldArray, futureArray) 

The second version (small c) has been deprecated which means it might not be available in future versions of Processing.
Smiley
Re: Swapping/copying arrays
Reply #5 - Nov 26th, 2009, 2:28pm
 
Good catch - I'll correct my post so people don't inadvertently copy it - and I look less stoopid Wink
Page Index Toggle Pages: 1