# Function changes an array it shouldn't interact with at all

**URL:** https://discourse.processing.org/t/function-changes-an-array-it-shouldnt-interact-with-at-all/42004
**Category:** Beginners
**Created:** [May 15, 2023, 2:12pm UTC](https://discourse.processing.org/t/function-changes-an-array-it-shouldnt-interact-with-at-all/42004 "2023-05-15T14:12:24Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Lednev](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@Lednev](https://discourse.processing.org/u/Lednev)
#### Post date: [May 15, 2023, 2:12pm UTC](https://discourse.processing.org/t/function-changes-an-array-it-shouldnt-interact-with-at-all/42004/1 "2023-05-15T14:12:24Z")

</div>

With th line marked with many “#” active i get this:

 ![sketch_230515b 15.05.2023 17_12_17](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/5/a/5a1d8caaf128647809d573b2c7783b4ae9fa3b54.png)

With it inactive i get this:

 ![sketch_230515b 15.05.2023 17_14_41](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/b/c/bc390a048d1435dc370595d07d24925cf86d3478.png)

This line and the line above it changes the p1 array somehow, even though it shouln’t, as far as i understand. What am i missing?

```auto
int points_number = 12;

PVector[] p1 = new PVector [points_number];
PVector[] p2 = new PVector [points_number];
//PVector[][] p3 = new PVector [ng][points_number/ng];

void setup () {
  size (800, 450);
  
  randomSeed(0);
  for (int i = 0; i < points_number; i++) {
    p1[i] = new PVector(random(width), random(height));
  }
  
  p2 = bsort (p1);
  
  for (int i = 0; i < points_number; i++) {
    fill (0);
    text (i, p1[i].x, p1[i].y);
  }
}

PVector[] bsort (PVector[] a) {
  PVector m;
  int n = 0;
  for (int i = 0; i < a.length; i++) {
    m = a[i];
    for (int j = i+1; j < a.length; j++) {
      if (m.x > a[j].x) {
  // a[n] = a[j];
        a[j] = m; // #######################################
  // n = j;
      }
    }
  }
  
  return a;
}

```

---

<div class="post-metadata">

### Author: ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)
#### Post date: [May 15, 2023, 2:49pm UTC](https://discourse.processing.org/t/function-changes-an-array-it-shouldnt-interact-with-at-all/42004/2 "2023-05-15T14:49:11Z")

</div>

Arrays are passed by-reference, not by-value. That means in your function, where you are sorting array `a`, you are sorting `p1` and returning it. When you assign that returned array to `p2`, you are wiping out the previously created array and causing the variable `p2` to also point to the same array as `p1`.

Instead, in your `bsort` function, you could create a new array `b`, copy the values from `a` into `b`, then sort and return array `b`, and that might give you what you expected.

---

<div class="post-metadata">

### Author: ![Lednev](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@Lednev](https://discourse.processing.org/u/Lednev)
#### Post date: [May 15, 2023, 3:07pm UTC](https://discourse.processing.org/t/function-changes-an-array-it-shouldnt-interact-with-at-all/42004/3 "2023-05-15T15:07:30Z")

</div>

Just tested it, changed the bsort() so that it creates a copy of a, processes and returns it and it didn’t work. I also tried to feed a copy of p1 into bsort(), it looked like this:

```auto
PVector[] a = p1;
p2 = bsort (a);

```

And it didn’t help either. Am i still missing something?  
Edit: by “and it didn’t work” i mean it didn’t lead to a different result

---

<div class="post-metadata">

### Author: ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)
#### Post date: [May 15, 2023, 3:27pm UTC](https://discourse.processing.org/t/function-changes-an-array-it-shouldnt-interact-with-at-all/42004/4 "2023-05-15T15:27:54Z")

</div>

Hi @Lednev,

This also isn’t a copy of the array, it is just an alias. Means by accessing a you access p1 by the alias a.

> [@Lednev](#):
>
> `PVector[] a = p1;`

check reference for deepcopy an array …

> **[arrayCopy() / Reference](https://processing.org/reference/arrayCopy_.html)**
>
> Copies an array (or part of an array) to another array. The src array is copied to the dst array, beginning at the position specified by srcPosition and into the position specifie…

Cheers  
— mnse

PS: don’t forget to allocate the target array as well with the same size as the source array…  
ie:

```auto
  String[] test = {"A","B","C"};
  String[] test2 = new String[test.length];
  arrayCopy(test,test2);
  printArray(test2);

```

console output:

```auto
[0] "A"
[1] "B"
[2] "C"

```

---

<div class="post-metadata">

### Author: ![Lednev](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@Lednev](https://discourse.processing.org/u/Lednev)
#### Post date: [May 15, 2023, 3:47pm UTC](https://discourse.processing.org/t/function-changes-an-array-it-shouldnt-interact-with-at-all/42004/5 "2023-05-15T15:47:59Z")

</div>

Okay, i think i got it. Thanks a lot!

---

<div class="post-metadata">

### Author: ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)
#### Post date: [May 15, 2023, 4:01pm UTC](https://discourse.processing.org/t/function-changes-an-array-it-shouldnt-interact-with-at-all/42004/6 "2023-05-15T16:01:20Z")

</div>

Think of computer memory as a city full of houses where you can store something in each house. An array variable, such as `int[] a` is an address to a row of houses. When you say `a = new int[5];`, you are asking the computer to find you five empty houses and give you their address. `int[] b` is another address. If you say `b = a;`, then both `a` and `b` have the same address to those same five houses. They don’t have a copy of the houses, they are the same five houses. If I say `a[2] = 17;` then `b[2]` will also be `17`.

When you pass `p1` into your `bsort()` function, you are passing the address of the array to the function. So any changes you make to elements of that array inside the function where it is called `a` are changing the same houses as those pointed to by `p1`. `arrayCopy()` is shorthand for creating a `new` array and then looping through the elements in the old array and copying them to the new array.

Even then, you still have to be careful when you have an array of objects. In your case, you have an array of PVectors. PVectors are objects. Objects are also really addresses to yet a different house somewhere else in memory. Even after you copy your array, if you modify, for instance, just the y component of one of your vectors, you’ll see the same change in both arrays. But if you replace one of the elements with a new PVector in one array, they will now point to two different PVectors and so can have separate values. If you were to create entirely new PVectors in your second array and fill them with the values of the vectors in the first, this is what’s called a “deep copy”. In your case, you do not need a deep copy, but you do want the shallow copy so that you can sort the second array without rearranging the first one.

---

<div class="post-metadata">

### Author: ![Lednev](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@Lednev](https://discourse.processing.org/u/Lednev)
#### Post date: [May 15, 2023, 4:23pm UTC](https://discourse.processing.org/t/function-changes-an-array-it-shouldnt-interact-with-at-all/42004/7 "2023-05-15T16:23:07Z")

</div>

That’s a great explanation, thanks. I have recently encountered the fact that i couldn’t manipulate array layers in c# as easily as in Processing, and i assume, the way Processing approaches arrays and the interacions between them allows for doing a lot of stuff much better performance-wise, while having drawbacks, such as the behaviour similar to what caused the problem i faced. Before i faced it, i thought, that by saying

```auto
int[] a = new int [5];
int[] b = a;

```

i was creating two separate arrays that are just equal. It seems strange though that i didn’t bump into this earlier, but it might explain many problems i left unsolved over the last couple years of occasionally fooling around in Processing. Thanks again!

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 15, 2023, 9:01pm UTC](https://discourse.processing.org/t/function-changes-an-array-it-shouldnt-interact-with-at-all/42004/8 "2023-05-15T21:01:25Z")

</div>

> [@Lednev](#):
>
> I was creating two separate arrays that are just equal.

- In Java, just declaring a variable doesn’t automatically create an object.
- Therefore this statement `int[] b = a;` isn’t creating anything!
- It’s merely declaring a variable named _b_ which is then initialized w/ the value of _a_.
- In the case above, the value of _a_ refers to the memory address of the array created by this expression `new int[5]`.
- That’s different from other languages such as C, which declaring a variable automatically creates its corresponding datatype object.
- In Java, declare variables & create objects are 2 different things.

> [@scudly](#):
>
> Arrays are passed by-reference, not by-value.

- Although that’s a valid PoV, officially Java passes everything by value.
- For object datatypes such as arrays or strings, those values are memory address, a.K.a. reference or pointer.
- Object datatypes are everything else which isn’t 1 of the 8 primitive datatypes:  
[https://docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html](https://docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)

---

<div class="post-metadata">

### Author: ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)
#### Post date: [May 15, 2023, 11:01pm UTC](https://discourse.processing.org/t/function-changes-an-array-it-shouldnt-interact-with-at-all/42004/9 "2023-05-15T23:01:04Z")

</div>

> > Arrays are passed by-reference, not by-value.
> 
> Although that’s a valid PoV, officially Java passes everything by value.  
> For object datatypes such as arrays or strings, those values are memory address, a.K.a. reference or pointer.  
> Object datatypes are everything else which isn’t 1 of the 8 primitive datatypes:  
> Primitive Data Types (The Java™ Tutorials \> Learning the Java Language \> Language Basics)

By that definition, all languages only pass by value. The point is, if the value being passed is the memory address of the data being referenced, we call it passing by-reference.

C/C++ and Java are no different in that regard. C/C++ just happens to have the additional mechanism where objects (or structs) can exist on the stack such that direct memory copies of them can be passed by value.

Arguably, “object reference” should be the 9th primitive datatype.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 16, 2023, 1:17am UTC](https://discourse.processing.org/t/function-changes-an-array-it-shouldnt-interact-with-at-all/42004/10 "2023-05-16T01:17:50Z")

</div>

> [@scudly](#):
>
> C/C++ and Java are no different in that regard.

That’s true. However, C/C++/D/C# can define functions which forcibly get the memory address of the variable argument itself rather than the value stored in that variable.

That’s what they call pass-by-reference. That is, the memory address of a variable.

While pass-by-value is when a function reads the content of a passed variable argument.
