Array Copy Concept Questions

R

Richard Forester

Hello.

I need some help understanding what goes on when an array is copied. I
create 2 arrays and copy one to the other:

int[] pins = {9, 3, 7, 2};
int[] copy = new int[pins.Length];

for (int i = 0; i != copy.Length; i++)

{

copy = pins;

}


As I understand it, the two arrays are pointing to the same data in memory
because this is a shallow copy. However, if I change one of the elements of
the "pins" array and then echo the values of each array to the screen they
are not the same. For example:

pins[0] = 0;

for (int i = 0; i != pins.Length; i++)

{

Console.WriteLine(pins);

}

for (int i = 0; i != copy.Length; i++)

{

Console.WriteLine(copy);

}


Is the 0th reference of "pins" now pointing to another INT in memory? If
so, why would a deep copy ever be valuable since the reference is changed
whenever the array is altered? Or am I just getting this all wrong?

Thanks for your help,
Richard
 
G

Guest

int is an integral data types therefore copy and pass as value and not by reference. When you did your loop that copied pins[] to copy[] you were actually doing a deep copy of the arrays. Each array after this point was pointing to their own copy of the integers. Changing one would not change the other.

To accomplish a shallow copy of int arrays simply assign copy[] to pins[]:

int[] pins = {9, 3, 7, 2};
int[] copy = null;

copy = pins; // shallow copy... copy is now referencing the same array object as pins

pins[0] = 0; // copy[0] now also = 0
 
J

Jacob Munk-Stander

Hi,
what you are doing is copying the values individually.

You have created a new int array (new int[pins.Length]) and copy the values
from pins into the copy array, essentially a deep copy.

If you want a shallow copy you'd just create a new int[] and assign it the
pins array, eg.

int[] copy2 = pins;

--
Yours sincerely,
Jacob Munk-Stander | http://jacob.munk-stander.dk


Richard Forester said:
Hello.

I need some help understanding what goes on when an array is copied. I
create 2 arrays and copy one to the other:

int[] pins = {9, 3, 7, 2};
int[] copy = new int[pins.Length];

for (int i = 0; i != copy.Length; i++)

{

copy = pins;

}


As I understand it, the two arrays are pointing to the same data in memory
because this is a shallow copy. However, if I change one of the elements of
the "pins" array and then echo the values of each array to the screen they
are not the same. For example:

pins[0] = 0;

for (int i = 0; i != pins.Length; i++)

{

Console.WriteLine(pins);

}

for (int i = 0; i != copy.Length; i++)

{

Console.WriteLine(copy);

}


Is the 0th reference of "pins" now pointing to another INT in memory? If
so, why would a deep copy ever be valuable since the reference is changed
whenever the array is altered? Or am I just getting this all wrong?

Thanks for your help,
Richard




----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption

=---
 
N

Nicole Calinoiu

Richard,

There are two things coming into play here:

1. You're using a value type, which will get copied.
2. You're changing the entire value, not operating on a member of the
"copied" instance.

You'll see the result you expected if you use a reference type and change a
property of one of the pins after making the copy.

HTH,
Nicole
 
J

Jon Skeet [C# MVP]

I need some help understanding what goes on when an array is copied. I
create 2 arrays and copy one to the other:

int[] pins = {9, 3, 7, 2};
int[] copy = new int[pins.Length];

for (int i = 0; i != copy.Length; i++)

{
copy = pins;
}


As I understand it, the two arrays are pointing to the same data in memory
because this is a shallow copy.


No, they're not. When an array is copied in the manner of the above,
the array itself has separate memory, but the value of each element in
the new array is the same as the value of each element in the old
array. When the array is an array of reference types, that means it's
only the reference which is copied - a copy of each object isn't
created.

For example, if instead of being an array of ints your array had been
an array of StringBuilders, and you'd appended some text to the
StringBuilder referred to by element 0 in the old array, you'd have
seen that via element 0 in the new array too, as they'd both still be
references to the same object.

You may find that http://www.pobox.com/~skeet/csharp/parameters.html
helps a bit on this.
 
R

Richard Forester

Thanks so much for your help. This was driving me crazy.

As a follow up, can you tell me if the same holds true if this were an array
of objects instead of ints?


C Addison Ritchie said:
int is an integral data types therefore copy and pass as value and not by
reference. When you did your loop that copied pins[] to copy[] you were
actually doing a deep copy of the arrays. Each array after this point was
pointing to their own copy of the integers. Changing one would not change
the other.
To accomplish a shallow copy of int arrays simply assign copy[] to pins[]:

int[] pins = {9, 3, 7, 2};
int[] copy = null;

copy = pins; // shallow copy... copy is now referencing the same array object as pins

pins[0] = 0; // copy[0] now also = 0

--
C Addison Ritchie, MCSD
Ritch Consulting, Inc.


Richard Forester said:
Hello.

I need some help understanding what goes on when an array is copied. I
create 2 arrays and copy one to the other:

int[] pins = {9, 3, 7, 2};
int[] copy = new int[pins.Length];

for (int i = 0; i != copy.Length; i++)

{

copy = pins;

}


As I understand it, the two arrays are pointing to the same data in memory
because this is a shallow copy. However, if I change one of the elements of
the "pins" array and then echo the values of each array to the screen they
are not the same. For example:

pins[0] = 0;

for (int i = 0; i != pins.Length; i++)

{

Console.WriteLine(pins);

}

for (int i = 0; i != copy.Length; i++)

{

Console.WriteLine(copy);

}


Is the 0th reference of "pins" now pointing to another INT in memory? If
so, why would a deep copy ever be valuable since the reference is changed
whenever the array is altered? Or am I just getting this all wrong?

Thanks for your help,
Richard




----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
R

Richard Forester

Many thanks to everyone who replied. I definitely understand how these work
now. The reason why I got so confused was a misleading sentence in the book
I am reading:

"No matter how you decide to copy an array, it's very important to realize
that an array copy is a shallow copy and not a deep copy."

:-(

Richard

Richard Forester said:
Hello.

I need some help understanding what goes on when an array is copied. I
create 2 arrays and copy one to the other:

int[] pins = {9, 3, 7, 2};
int[] copy = new int[pins.Length];

for (int i = 0; i != copy.Length; i++)

{

copy = pins;

}


As I understand it, the two arrays are pointing to the same data in memory
because this is a shallow copy. However, if I change one of the elements of
the "pins" array and then echo the values of each array to the screen they
are not the same. For example:

pins[0] = 0;

for (int i = 0; i != pins.Length; i++)

{

Console.WriteLine(pins);

}

for (int i = 0; i != copy.Length; i++)

{

Console.WriteLine(copy);

}


Is the 0th reference of "pins" now pointing to another INT in memory? If
so, why would a deep copy ever be valuable since the reference is changed
whenever the array is altered? Or am I just getting this all wrong?

Thanks for your help,
Richard




----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption

=---
 
J

Jon Skeet [C# MVP]

Many thanks to everyone who replied. I definitely understand how these work
now. The reason why I got so confused was a misleading sentence in the book
I am reading:

"No matter how you decide to copy an array, it's very important to realize
that an array copy is a shallow copy and not a deep copy."

And that's correct - it's a shallow copy in that it just copies the
values in the array, not the objects that those values might refer to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top