473,787 Members | 2,971 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne(pins[i]);

}

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

{

Console.WriteLi ne(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 1729
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.WriteLi ne(pins[i]);

}

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

{

Console.WriteLi ne(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******@m sn.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.WriteLi ne(pins[i]);

}

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

{

Console.WriteLi ne(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******@m sn.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.WriteLi ne(pins[i]);

}

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

{

Console.WriteLi ne(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.m icrosoft.com> wrote in
message news:93******** *************** ***********@mic rosoft.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.WriteLi ne(pins[i]);

}

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

{

Console.WriteLi ne(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******@m sn.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.co m>
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******@m sn.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.WriteLi ne(pins[i]);

}

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

{

Console.WriteLi ne(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******@m sn.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.co m>
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
1050
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 how to use the Array.Copy method to accomplish this but it seems to only work for single dimension arrays. Here is a code sample: 'dim arrTest(600,3) as string 'Actual array dimension needed dim arrTest(600) as string 'Used only in this test...
4
3318
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
2057
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); and have an array of cloned objects (i.e. the new array of objects aren't the objects contained in my original array). But instead it seems necessary for me to have to iterate over my entire array and individually clone each element in the...
0
1973
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 end (seperated by commas). I've also tried the copyTo and can't get that to work either. Array.Copy(str, gradesArray, 3) Why do I keep getting this message? Argument Null Exception was Handled.
1
8406
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 moves the content with the system call m_memmove. According to the documentation though, Buffer.BlockCopy works with primitive types such as byte, char, etc. But I've found a case where Buffer.BlockCopy doesn't work as it should (or maybe I'm missing...
8
2173
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 (where there is an
8
7445
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 realDataSent is now copy to the beginning of the sendBuffer. I have tried: Array.Copy(sendBuffer, realDataSent, sendBuffer, 0, sendBuffer.Length - realDataSent);
7
3630
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 < recordLen; j++, i++) { dest = src; } }
10
3374
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
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.