468,768 Members | 1,673 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,768 developers. It's quick & easy.

Array Copy Concept Questions

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[i] = pins[i];

}
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[i]);

}

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

{

Console.WriteLine(copy[i]);

}
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 =---
Nov 16 '05 #1
7 1525
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" wrote:
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[i] = pins[i];

}
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[i]);

}

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

{

Console.WriteLine(copy[i]);

}
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 =---

Nov 16 '05 #2
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" <richard_(no-spam)fo******@msn.com> wrote in message
news:41**********@127.0.0.1...
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[i] = pins[i];

}
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[i]);

}

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

{

Console.WriteLine(copy[i]);

}
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

=---
Nov 16 '05 #3
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
"Richard Forester" <richard_(no-spam)fo******@msn.com> wrote in message
news:41**********@127.0.0.1...
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[i] = pins[i];

}
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[i]);

}

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

{

Console.WriteLine(copy[i]);

}
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
=---

Nov 16 '05 #4
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" <CA*************@discussions.microsoft.com> wrote in
message news:93**********************************@microsof t.com...
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" wrote:
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[i] = pins[i];

}
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[i]);

}

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

{

Console.WriteLine(copy[i]);

}
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 =---



----== 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 =---
Nov 16 '05 #5
<"Richard Forester" <richard_(no-spam)fo******@msn.com>> wrote:
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[i] = pins[i];
}
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
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" <richard_(no-spam)fo******@msn.com> wrote in message
news:41**********@127.0.0.1...
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[i] = pins[i];

}
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[i]);

}

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

{

Console.WriteLine(copy[i]);

}
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

=---


----== 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 =---
Nov 16 '05 #7
<"Richard Forester" <richard_(no-spam)fo******@msn.com>> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by rmorvay | last post: by
4 posts views Thread by Gidon Sela | last post: by
reply views Thread by Favre Fan | last post: by
8 posts views Thread by anon.asdf | last post: by
8 posts views Thread by linuxfedora | last post: by
7 posts views Thread by fr33host | last post: by
10 posts views Thread by Alan Mosley | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.