473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1703
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: rmorvay | last post by:
I have a requirement to search a multi-dimensional array for an item, then delete the item and "reset" the array so that their are no gaps in the resulting array. I have been trying to figure out...
4
by: Gidon Sela | last post by:
Is the Array.Copy Method only limited to copying one dimensional arrays? If this is not the case, what is the syntax to copy multi-dimensional arrays with this method?
1
by: illegal.prime | last post by:
I would like to have an array of objects (whose class I define) and then just invoke either: MyClass clonedArray = (MyClass) myArray.Clone(); OR Array.Copy(myArray, clonedArray, myArray.Length);...
0
by: Favre Fan | last post by:
Can anyone help me with the array.copy? I have an array coming in from a text file. Info is being read in with my str() string array I then want to copy that array from the third element to the...
1
by: kellox | last post by:
Does anybody know the difference between the two static methods Buffer.BlockCopy and Array.Copy? It is said that Buffer.BlockCopy is faster than Array.Copy since it only checks boundary and then...
8
by: anon.asdf | last post by:
Hi! OK, lets try "array-copy": { char arrayA; arrayA = (char){1, 2, 3}; } it does *not* work since we're trying to make a fixed array-pointer arrayA, point to another location/address...
8
by: linuxfedora | last post by:
Which one is faster or any other better way to do it. I have an array of byte with name: sendBuffer, and i will like to make some thing like that the value started from index of the array in...
7
by: fr33host | last post by:
Dear developers, I have a question. Is there a faster array copy than a for loop. What would you suggest for this code sample? void get(char* src, char* dest, int i) { for (int j = 0; j <...
10
by: Alan Mosley | last post by:
I have a multi dimensional array, I want to copy, but when I do It shows 0's in the second dimension. Can I copy a multi dimensional array? if so how do i do it. Thanks
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.