Arrays, shallow copy vs. deep copy

J

joels

I'm a bit confused by the behavior of arrays and exactly when a
shallow vs. deep copy is made.

When passing an array of value types (e.g., int), msdn states that a
deep copy is NOT made, thus, one would infer that this works exactly
like the Clone method of the array class; i.e., creating a shallow
copy.

When calling the Clone method on an array, the cloned copy is a
shallow copy of the original, thus, the new (cloned) array should
refer to the same set of values, yes? However, this is not the case
when the array contains value types.

For example, if I'm passing an array to a method and increment the
first item in the array, the original array reflects that change.
HOWEVER, if I clone the array, and then increment the first item in
the reference which holds the cloned array, the original does NOT
reflect the change-- unlike the change noticed when passing an array
as a parameter.

HOWEVER, this all changes when the array holds object references, in
which case changes to the array reference holding the cloned array are
reflected in the original.

I'm looking for the internals workings here, I hope I was clear in my
discussion.

Thanks for any help,
Joel
 
M

Mattias Sjögren

Joel,
When passing an array of value types (e.g., int), msdn states that a
deep copy is NOT made

Correct, since arrays are reference types.

one would infer that this works exactly
like the Clone method of the array class

No, and I'm not sure how you came to that conclusion.

the new (cloned) array should
refer to the same set of values, yes?

For arrays of value types, there's no "refering" going on. The array
holds the values, not references/pointers to them. So if you clone the
array you get a new set of data.

HOWEVER, this all changes when the array holds object references, in
which case changes to the array reference holding the cloned array are
reflected in the original.

Right, because the array in this case holds object references.



Mattias
 
G

Guest

there are a few things you need to clear up. first, Array itself is a reference type. so when you pass an array as parameter to another method, it behaves just like any reference type would. a new reference is passed pointing to the same array instance on the heap. no copying is performed. any change to the content of the array is obviously persisted after the method done executing. nothing to do with array cloning

second, an array of value type will have the actual values inlined with the array on the heap, while an array of reference type will only have the references inlined with the array, but not what they are referencing. when you clone the array, whatever is inlined with the array gets cloned and stored inline with the new array. so in the case of value types, the actual values are copied, *essentially a deep copy in spirit*, while with reference types, only the references are copied, the shallow part

----- joels wrote: ----

I'm a bit confused by the behavior of arrays and exactly when
shallow vs. deep copy is made

When passing an array of value types (e.g., int), msdn states that
deep copy is NOT made, thus, one would infer that this works exactl
like the Clone method of the array class; i.e., creating a shallo
copy

When calling the Clone method on an array, the cloned copy is
shallow copy of the original, thus, the new (cloned) array shoul
refer to the same set of values, yes? However, this is not the cas
when the array contains value types

For example, if I'm passing an array to a method and increment th
first item in the array, the original array reflects that change.
HOWEVER, if I clone the array, and then increment the first item i
the reference which holds the cloned array, the original does NO
reflect the change-- unlike the change noticed when passing an arra
as a parameter

HOWEVER, this all changes when the array holds object references, i
which case changes to the array reference holding the cloned array ar
reflected in the original

I'm looking for the internals workings here, I hope I was clear in m
discussion

Thanks for any help
Joe
 
J

Joel Scavone

Thanks much.

I ASSUMED that since the Clone method of the array class was used to
make a shallow copy, and since passing an array to a method was also
using a shallow copy, the internal mechanisms were the same.

My assumption is, or course, wrong.

Your comments cleared things up well. Thanks, again.

Joel
 

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