What is the difference between a deep copy and a shallow copy?
Asked Answered
S

31

753

What is the difference between a deep copy and a shallow copy?

Stelly answered 8/10, 2008 at 20:22 Comment(0)
F
903

Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.

Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated.

Flemish answered 8/10, 2008 at 20:29 Comment(9)
May be .NET MemberwiseClone() implementation do more than shallow copying in the conventional senseCephalic
Keep in mind there are also mixed copies (not only such as lazy copy), which duplicates just part of it (here's an instance)! ;)Undertaker
So a shallow copy of X can change the elements in X but a deep copy cannot?Skirting
what is a collection structure?Confectionery
@Honey Collections can be diverse data structures which stores multiple data items. In python we have tuple,list,dictionery,etcPioneer
As most things, this is a bit language dependent. For example, JavaScript's definition of shallow copies is copying all primitive values and creating references to objects.Ipsambul
@AugustinePA, that depends on the nature of the collection. If you make a shallow copy of an int[] in Java for instance, the elements will indeed be copied.Sofia
@RoyiNamir You probably already figured this out during the last 7 years, but for anybody else wondering about this: "shallow copy copies the value type bit by bit" is correct, but it's a bit confusing. If you have a Customer object which "has" an Address object, copying the Customer object "bit by bit" means that the pointer/reference to the Address object is copied. Original and copy both point to the same Address object, whereas a deep copy will create a new Address object and point to that instead.Bennington
Also see this answer and this answer for examples.Become
W
1053

Breadth vs Depth; think in terms of a tree of references with your object as the root node.

Shallow:

Before Copy Shallow Copying Shallow Done

The variables A and B refer to different areas of memory, when B is assigned to A the two variables refer to the same area of memory. Later modifications to the contents of either are instantly reflected in the contents of other, as they share contents.

Deep:

Before Copy Deep Copying Deep Done

The variables A and B refer to different areas of memory, when B is assigned to A the values in the memory area which A points to are copied into the memory area to which B points. Later modifications to the contents of either remain unique to A or B; the contents are not shared.

Whoredom answered 8/10, 2008 at 20:39 Comment(12)
Here's the wikipedia article that this illustration comes from in case it doesn't make sense out of context for you en.wikipedia.org/wiki/Object_copy#Shallow_copyLitigable
In case of shallow copy if we make any changes in array B will that be reflected in array A since A & B both point to same memory location ?Somatic
So shallow copy means only a reference to the original entity is available to the so-called-copy. Any change to the original entity will be replicatedto the new one? Deep copy would clone the original entity and make a new unrelated copy.Instability
In single line its copy by reference vs copy by value. Not sure if the answer is correct!Glyconeogenesis
images directly taken from wikipedia without citationAssessor
@Assessor So 9 years ago I just put urls to the images because embedding images wasn't supported. Thus the URL cited its source. The community later made the URLs into embedded images without editing some kind of citation onto it. The 4 year old top comment also points out what you point out. Have a look: stackoverflow.com/posts/184780/revisions Why not just edit a citation into the answer yourself? I might be unavailable the next time someone has some complaint about my 10 year old writing style.Whoredom
Sure, I will add the citation that you left off. Regardless of the person I will continue to make it known if I see anyone using another's work without a citation, because it's the right thing to do. Though in fairness to your response, you could have added a citation just as fast as writing a response. It's good form to cite sources that aren't yours on principle. Best wishes.Assessor
"For variables A and B, when B is assigned to A" doesn't this mean "A = B" in code? I'm somehow confused, since the image reflects "B = A".Jaunitajaunt
As @Mannu suggests, this answer explains copy by reference vs copy by value. The shallow vs deep copy concept is applicable to collections that have references as elements. If the elements of both the old and new arrays are references to the same objects then it is shallow copy. If the references are dereferenced to create new objects then it is deep copy. Basically if new objects are created recursively then it is deep copy. This is what is explained in the accepted answer. This answer explains it well using examples.Become
@chetanraina totally agree, just an object is a collection of 0 order. What you do to an object (reference or value copying) is applicable to each part of the collection when applying shallow or deep copying respectively. I tried to keep it super brief as a summary of the easy to find wikipedia article or maybe someone's textbook page.Whoredom
@Whoredom there is no problem as long as we are talking about singular objects that are not collections. But it gets wrong in the context of collections. If we consider copying of collections, then what you have shown as deep copy is actually shallow copy (both collections have the same references X1, X2, X3) and what you have shown as shallow copy is not actually copying because no copy was created, only a new reference was assigned to B.Become
@PeterGuan That depends how you look at it. With the current wording both can be true. If I have box A and box B the expression "B is assigned to A" doesn't make a lot of sense. In the answer the author meant "B is assigned to the content of A" where the content is the reference to some structure. ("the content of A is assigned to B" would also work, but goes against the answer here.)Stepheniestephens
F
903

Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.

Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated.

Flemish answered 8/10, 2008 at 20:29 Comment(9)
May be .NET MemberwiseClone() implementation do more than shallow copying in the conventional senseCephalic
Keep in mind there are also mixed copies (not only such as lazy copy), which duplicates just part of it (here's an instance)! ;)Undertaker
So a shallow copy of X can change the elements in X but a deep copy cannot?Skirting
what is a collection structure?Confectionery
@Honey Collections can be diverse data structures which stores multiple data items. In python we have tuple,list,dictionery,etcPioneer
As most things, this is a bit language dependent. For example, JavaScript's definition of shallow copies is copying all primitive values and creating references to objects.Ipsambul
@AugustinePA, that depends on the nature of the collection. If you make a shallow copy of an int[] in Java for instance, the elements will indeed be copied.Sofia
@RoyiNamir You probably already figured this out during the last 7 years, but for anybody else wondering about this: "shallow copy copies the value type bit by bit" is correct, but it's a bit confusing. If you have a Customer object which "has" an Address object, copying the Customer object "bit by bit" means that the pointer/reference to the Address object is copied. Original and copy both point to the same Address object, whereas a deep copy will create a new Address object and point to that instead.Bennington
Also see this answer and this answer for examples.Become
B
229

Try to consider following image

enter image description here

For example Object.MemberwiseClone creates a shallow copy link

and using ICloneable interface you can get deep copy as described here

Bertrand answered 15/6, 2016 at 8:42 Comment(5)
A picture is worth a thousand words.Trimolecular
Oh boy, came here to find out the meaning. This is the only answer which helped.Simmers
This is the simplest and yet only shows what's necessary.Intercalation
This answer explains copy by reference vs copy by value. Shallow copy vs deep copy is a concept that applies to collections. See this answer and this answer.Become
The concept applies to much more than collectionsStrouse
E
181

In short, it depends on what points to what. In a shallow copy, object B points to object A's location in memory. In deep copy, all things in object A's memory location get copied to object B's memory location.

The Wikipedia article Object copy has a great diagram.

Ergosterol answered 8/10, 2008 at 20:24 Comment(0)
S
72

Especially For iOS Developers:

If B is a shallow copy of A, then for primitive data it's like B = [A assign]; and for objects it's like B = [A retain];

B and A point to the same memory location

If B is a deep copy of A, then it is like B = [A copy];

B and A point to different memory locations

B memory address is same as A's

B has same contents as A's

Subnormal answered 23/1, 2013 at 11:43 Comment(2)
"B memory address is same as A's" - How come ?Rentsch
In Deep Copy, "B memory address is NOT same as A's"Masaccio
G
69

Shallow copy: Copies the member values from one object into another.

Deep Copy:    Copies the member values from one object into another.
                     Any pointer objects are duplicated and Deep Copied.

Example:

class String
{
     int   size;
     char* data;
};

String  s1("Ace");   // s1.size = 3 s1.data=0x0000F000

String  s2 = shallowCopy(s1);
 // s2.size =3 s2.data = 0X0000F000
String  s3 = deepCopy(s1);
 // s3.size =3 s3.data = 0x0000F00F
 //                      (With Ace copied to this location.)
Godinez answered 8/10, 2008 at 20:25 Comment(0)
C
62

Just for the sake of easy understanding you could follow this article: https://www.cs.utexas.edu/~scottm/cs307/handouts/deepCopying.htm


Shallow Copy:

Shallow Copy


Deep Copy:

Deep Copy

Chalaza answered 19/3, 2014 at 6:15 Comment(0)
D
52

I haven't seen a short, easy to understand answer here--so I'll give it a try.

With a shallow copy, any object pointed to by the source is also pointed to by the destination (so that no referenced objects are copied).

With a deep copy, any object pointed to by the source is copied and the copy is pointed to by the destination (so there will now be 2 of each referenced object). This recurses down the object tree.

Declassify answered 8/10, 2008 at 20:36 Comment(0)
B
37
char * Source = "Hello, world.";

char * ShallowCopy = Source;    

char * DeepCopy = new char(strlen(Source)+1);
strcpy(DeepCopy,Source);        

'ShallowCopy' points to the same location in memory as 'Source' does. 'DeepCopy' points to a different location in memory, but the contents are the same.

Baca answered 8/10, 2008 at 20:32 Comment(0)
I
37

{Imagine two objects: A and B of same type _t(with respect to C++) and you are thinking about shallow/deep copying A to B}

Shallow Copy: Simply makes a copy of the reference to A into B. Think about it as a copy of A's Address. So, the addresses of A and B will be the same i.e. they will be pointing to the same memory location i.e. data contents.

Deep copy: Simply makes a copy of all the members of A, allocates memory in a different location for B and then assigns the copied members to B to achieve deep copy. In this way, if A becomes non-existant B is still valid in the memory. The correct term to use would be cloning, where you know that they both are totally the same, but yet different (i.e. stored as two different entities in the memory space). You can also provide your clone wrapper where you can decide via inclusion/exclusion list which properties to select during deep copy. This is quite a common practice when you create APIs.

You can choose to do a Shallow Copy ONLY_IF you understand the stakes involved. When you have enormous number of pointers to deal with in C++ or C, doing a shallow copy of an object is REALLY a bad idea.

EXAMPLE_OF_DEEP COPY_ An example is, when you are trying to do image processing and object recognition you need to mask "Irrelevant and Repetitive Motion" out of your processing areas. If you are using image pointers, then you might have the specification to save those mask images. NOW... if you do a shallow copy of the image, when the pointer references are KILLED from the stack, you lost the reference and its copy i.e. there will be a runtime error of access violation at some point. In this case, what you need is a deep copy of your image by CLONING it. In this way you can retrieve the masks in case you need them in the future.

EXAMPLE_OF_SHALLOW_COPY I really think it is not a good idea to do shallow copy if you know that your program is gonna run for an infinite period of time i.e. continuous "push-pop" operation over the stack with function calls. If you are demonstrating something to an amateur or novice person (e.g. C/C++ tutorial stuff) then it is probably okay. But if you are running an application such as surveillance and detection system, or Sonar Tracking System, you are not supposed to keep shallow copying your objects around because it will kill your program sooner or later.

Inutility answered 31/1, 2013 at 23:39 Comment(0)
H
37

What is Shallow Copy?

Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, only the reference addresses are copied i.e., only the memory address is copied. Shallow Copy

In this figure, the MainObject1 has fields field1 of type int, and ContainObject1 of type ContainObject. When you do a shallow copy of MainObject1, MainObject2 is created with field2 containing the copied value of field1 and still pointing to ContainObject1 itself. Note that since field1 is of primitive type, its value is copied to field2 but since ContainedObject1 is an object, MainObject2 still points to ContainObject1. So any changes made to ContainObject1 in MainObject1 will be reflected in MainObject2.

Now if this is shallow copy, lets see what's deep copy?

What is Deep Copy?

A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. A deep copy occurs when an object is copied along with the objects to which it refers. Deep Copy

In this figure, the MainObject1 have fields field1 of type int, and ContainObject1 of type ContainObject. When you do a deep copy of MainObject1, MainObject2 is created with field2 containing the copied value of field1 and ContainObject2 containing the copied value of ContainObject1. Note any changes made to ContainObject1 in MainObject1 will not reflect in MainObject2.

good article

Hugmetight answered 18/12, 2014 at 6:55 Comment(1)
it's not your fault though this example refers to a field3 which when in a position to try and comprehend something as deep as that issue, where is that #3 in that example taking place ContainObject2 ?Diarmuid
F
17

In object oriented programming, a type includes a collection of member fields. These fields may be stored either by value or by reference (i.e., a pointer to a value).

In a shallow copy, a new instance of the type is created and the values are copied into the new instance. The reference pointers are also copied just like the values. Therefore, the references are pointing to the original objects. Any changes to the members that are stored by reference appear in both the original and the copy, since no copy was made of the referenced object.

In a deep copy, the fields that are stored by value are copied as before, but the pointers to objects stored by reference are not copied. Instead, a deep copy is made of the referenced object, and a pointer to the new object is stored. Any changes that are made to those referenced objects will not affect other copies of the object.

Frontolysis answered 8/10, 2008 at 20:57 Comment(0)
C
17

Deep Copy

A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. A deep copy occurs when an object is copied along with the objects to which it refers.

Shallow Copy

Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, just the reference addresses are copied i.e., only the memory address is copied.

Codicodices answered 16/9, 2014 at 9:50 Comment(1)
That link sadly no longer works - it now points to an article from February 2019 regarding web design (unless the author is clairvoyant?).Ginger
C
15

I would like to give example rather than the formal definition.

var originalObject = { 
    a : 1, 
    b : 2, 
    c : 3,
};

This code shows a shallow copy:

var copyObject1 = originalObject;

console.log(copyObject1.a);         // it will print 1 
console.log(originalObject.a);       // it will also print 1 
copyObject1.a = 4; 
console.log(copyObject1.a);           //now it will print 4 
console.log(originalObject.a);       // now it will also print 4

var copyObject2 = Object.assign({}, originalObject);

console.log(copyObject2.a);        // it will print 1 
console.log(originalObject.a);      // it will also print 1 
copyObject2.a = 4; 
console.log(copyObject2.a);        // now it will print 4 
console.log(originalObject.a);      // now it will print 1

This code shows a deep copy:

var copyObject2 = Object.assign({}, originalObject);

console.log(copyObject2.a);        // it will print 1 
console.log(originalObject.a);      // it will also print 1 
copyObject2.a = 4; 
console.log(copyObject2.a);        // now it will print 4 
console.log(originalObject.a);      // !! now it will print 1 !!
Coff answered 24/1, 2018 at 6:52 Comment(3)
I am getting 1 1 4 4 4 4 4 4Peyter
in deep copy, do copyObject.a = 8 and then check. hope you will get proper answer.Coff
object.assign({},arr) will not create the deep copy ,suppose we have the following object var source = {"foo":1,"name":"Testing",c:{age:34}} var dCopy = Object.assign({},source) console.log(dCopy.c.age) console.log(Source deep ${source.c.age}) source.c.age = 3 console.log(dCopy.c.age) console.log(Source deep ${source.c.age})Transmigrant
P
13

'ShallowCopy' points to the same location in memory as 'Source' does. 'DeepCopy' points to a different location in memory, but the contents are the same.

Palomino answered 21/2, 2011 at 13:44 Comment(1)
This is slightly misleading. Both a shallow and deep copy will copy the object to a new location in memory, a deep will also copy the child objects whereas a shallow will just have the new objects refer to the old children. It's difficult to read without referring to the original object.Declassify
T
12

Shallow Cloning:
Definition: "A shallow copy of an object copies the ‘main’ object, but doesn’t copy the inner objects." When a custom object (eg. Employee) has just primitive, String type variables then you use Shallow Cloning.

Employee e = new Employee(2, "john cena");
Employee e2=e.clone();

You return super.clone(); in the overridden clone() method and your job is over.

Deep Cloning:
Definition: "Unlike the shallow copy, a deep copy is a fully independent copy of an object."
Means when an Employee object holds another custom object:

Employee e = new Employee(2, "john cena", new Address(12, "West Newbury", "Massachusetts");

Then you have to write the code to clone the 'Address' object as well in the overridden clone() method. Otherwise the Address object won't clone and it causes a bug when you change value of Address in cloned Employee object, which reflects the original one too.

Teletypesetter answered 26/9, 2017 at 4:29 Comment(0)
A
10
var source = { firstName="Jane", lastname="Jones" };
var shallow = ShallowCopyOf(source);
var deep = DeepCopyOf(source);
source.lastName = "Smith";
WriteLine(source.lastName); // prints Smith
WriteLine(shallow.lastName); // prints Smith
WriteLine(deep.lastName); // prints Jones
Abomb answered 8/10, 2008 at 20:34 Comment(2)
That's not a good example. Shallow copies are mostly used for quick copying of objects, without copying the data, but once an objects needs to modify the shared data, a deep copy of it is taken. Your example will likely confuse beginners.Charles
this only works in languages that use pointers to represent strings. The point that DHA is trying to make is that shallow copy only duplicates pointers to the identical (singular) original content, while deep copy clones the referenced content of the pointers as well. Both methods copy surface content. If the language stores strings as surface literal content, e.g. inside a WAV header, this example will not work. Note this is probably too picky for most real-life problems that are not esoteric.Duppy
E
10

Shallow Copy- Reference variable inside original and shallow-copied objects have reference to common object.

Deep Copy- Reference variable inside original and deep-copied objects have reference to different object.

clone always does shallow copy.

public class Language implements Cloneable{
    
    String name;
    public Language(String name){
        this.name=name;
    }
    
    public String getName() {
        return name;
    }
    
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

main class is following-

public static void main(String args[]) throws ClassNotFoundException, CloneNotSupportedException{

      ArrayList<Language> list=new ArrayList<Language>();
      list.add(new Language("C"));
      list.add(new Language("JAVA"));

      ArrayList<Language> shallow=(ArrayList<Language>) list.clone();
      //We used here clone since this always shallow copied.

      System.out.println(list==shallow);
      
      for(int i=0;i<list.size();i++)
      System.out.println(list.get(i)==shallow.get(i));//true
      
      ArrayList<Language> deep=new ArrayList<Language>();
      for(Language language:list){
          deep.add((Language) language.clone());
      }
      System.out.println(list==deep);
      for(int i=0;i<list.size();i++)
          System.out.println(list.get(i)==deep.get(i));//false
      
} 

OutPut of above will be-

false true true

false false false

Any change made in origional object will reflect in shallow object not in deep object.

  list.get(0).name="ViSuaLBaSiC";
  System.out.println(shallow.get(0).getName()+"  "+deep.get(0).getName());

OutPut- ViSuaLBaSiC C

Erosion answered 7/5, 2015 at 7:12 Comment(0)
C
8

Imagine there are two arrays called arr1 and arr2.

arr1 = arr2;   //shallow copy
arr1 = arr2.clone(); //deep copy
Corso answered 29/5, 2015 at 0:41 Comment(0)
L
8

A shallow copy constructs a new compound object and insert its references into it to the original object.

Unlike shallow copy, deepcopy constructs new compound object and also inserts copies of the original objects of original compound object.

Lets take an example.

import copy
x =[1,[2]]
y=copy.copy(x)
z= copy.deepcopy(x)
print(y is z)

Above code prints FALSE.

Let see how.

Original compound object x=[1,[2]] (called as compound because it has object inside object (Inception))

enter image description here

as you can see in the image, there is a list inside list.

Then we create a shallow copy of it using y = copy.copy(x). What python does here is, it will create a new compound object but objects inside them are pointing to the orignal objects.

enter image description here

In the image it has created a new copy for outer list. but the inner list remains same as the original one.

Now we create deepcopy of it using z = copy.deepcopy(x). what python does here is, it will create new object for outer list as well as inner list. as shown in the image below (red highlighted).

enter image description here

At the end code prints False, as y and z are not same objects.

Larentia answered 20/1, 2018 at 7:37 Comment(0)
H
7

In Simple Terms, a Shallow Copy is similar to Call By Reference and a Deep Copy is similar to Call By Value

In Call By Reference, Both formal and actual parameters of a function refers to same memory location and the value.

In Call By Value, Both formal and actual parameters of a functions refers to different memory location but having the same value.

Handshake answered 2/12, 2011 at 0:55 Comment(0)
G
5
struct sample
{
    char * ptr;
}
void shallowcpy(sample & dest, sample & src)
{
    dest.ptr=src.ptr;
}
void deepcpy(sample & dest, sample & src)
{
    dest.ptr=malloc(strlen(src.ptr)+1);
    memcpy(dest.ptr,src.ptr);
}
Gujranwala answered 26/8, 2011 at 6:33 Comment(0)
A
4

To add more to other answers,

  • a Shallow Copy of an object performs copy by value for value types based properties, and copy by reference for reference types based properties.
  • a Deep Copy of an object performs copy by value for value types based properties, as well as copy by value for reference types based properties deep in the hierarchy (of reference types)
Aiaia answered 29/9, 2016 at 16:44 Comment(0)
L
4

Shallow copy will not create new reference but deep copy will create the new reference.

Here is the program to explain the deep and shallow copy.

public class DeepAndShollowCopy {
    int id;
    String name;
    List<String> testlist = new ArrayList<>();

    /*
    // To performing Shallow Copy 
    // Note: Here we are not creating any references. 
      public DeepAndShollowCopy(int id, String name, List<String>testlist)
       { 

       System.out.println("Shallow Copy for Object initialization");
       this.id = id; 
       this.name = name; 
       this.testlist = testlist; 

       }
    */  

    // To performing Deep Copy 
    // Note: Here we are creating one references( Al arraylist object ). 
    public DeepAndShollowCopy(int id, String name, List<String> testlist) {
        System.out.println("Deep Copy for Object initialization");
        this.id = id;
        this.name = name;
        String item;
        List<String> Al = new ArrayList<>();
        Iterator<String> itr = testlist.iterator();
        while (itr.hasNext()) {
            item = itr.next();
            Al.add(item);
        }
        this.testlist = Al;
    }


    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Java");
        list.add("Oracle");
        list.add("C++");
        DeepAndShollowCopy copy=new DeepAndShollowCopy(10,"Testing", list);
        System.out.println(copy.toString());
    }
    @Override
    public String toString() {
        return "DeepAndShollowCopy [id=" + id + ", name=" + name + ", testlist=" + testlist + "]";
    }
}
Leuco answered 6/3, 2017 at 9:50 Comment(0)
S
3

Taken from [blog]: http://sickprogrammersarea.blogspot.in/2014/03/technical-interview-questions-on-c_6.html

Deep copy involves using the contents of one object to create another instance of the same class. In a deep copy, the two objects may contain ht same information but the target object will have its own buffers and resources. the destruction of either object will not affect the remaining object. The overloaded assignment operator would create a deep copy of objects.

Shallow copy involves copying the contents of one object into another instance of the same class thus creating a mirror image. Owing to straight copying of references and pointers, the two objects will share the same externally contained contents of the other object to be unpredictable.

Explanation:

Using a copy constructor we simply copy the data values member by member. This method of copying is called shallow copy. If the object is a simple class, comprised of built in types and no pointers this would be acceptable. This function would use the values and the objects and its behavior would not be altered with a shallow copy, only the addresses of pointers that are members are copied and not the value the address is pointing to. The data values of the object would then be inadvertently altered by the function. When the function goes out of scope, the copy of the object with all its data is popped off the stack.

If the object has any pointers a deep copy needs to be executed. With the deep copy of an object, memory is allocated for the object in free store and the elements pointed to are copied. A deep copy is used for objects that are returned from a function.

Skimp answered 7/3, 2014 at 8:39 Comment(0)
S
3

I came to understand from the following lines.

Shallow copy copies an object value type(int, float, bool) fields in to target object and object's reference types(string, class etc) are copied as references in target object. In this target reference types will be pointing to the memory location of source object.

Deep copy copies an object's value and reference types into a complete new copy of the target objects. This means both the value types and reference types will be allocated a new memory locations.

Seeley answered 28/7, 2018 at 12:54 Comment(0)
H
2

Shallow copying is creating a new object and then copying the non-static fields of the current object to the new object. If a field is a value type --> a bit-by-bit copy of the field is performed; for a reference type --> the reference is copied but the referred object is not; therefore the original object and its clone refer to the same object.

Deep copy is creating a new object and then copying the nonstatic fields of the current object to the new object. If a field is a value type --> a bit-by-bit copy of the field is performed. If a field is a reference type --> a new copy of the referred object is performed. The classes to be cloned must be flagged as [Serializable].

Hammett answered 8/8, 2013 at 8:32 Comment(0)
S
1

Copying ararys :

Array is a class, which means it is reference type so array1 = array2 results in two variables that reference the same array.

But look at this example:

  static void Main()
    {
        int[] arr1 = new int[] { 1, 2, 3, 4, 5 }; 
        int[] arr2 = new int[] { 6, 7, 8, 9, 0 };

        Console.WriteLine(arr1[2] + " " + arr2[2]);
        arr2 = arr1;
        Console.WriteLine(arr1[2] + " " + arr2[2]); 
        arr2 = (int[])arr1.Clone();
        arr1[2] = 12;
        Console.WriteLine(arr1[2] + " " + arr2[2]);
    }

shallow clone means that only the memory represented by the cloned array is copied.

If the array contains value type objects, the values are copied;

if the array contains reference type, only the references are copied - so as a result there are two arrays whose members reference the same objects.

To create a deep copy—where reference type are duplicated, you must loop through the array and clone each element manually.

Simonsimona answered 19/12, 2012 at 21:23 Comment(2)
I don't know about other languages, but in C#/VB, shallow copying an array of value types does not copy the values. The two arrays refer to the same objects. Add a button to a form and add this code to see: private void button1_Click(object sender, EventArgs e) { int[] arr1 = new int[]{1,2,3,4,5}; int[] arr2 = new int[]{6,7,8,9,0}; MessageBox.Show(arr1[2] + " " + arr2[2]); arr2 = arr1; MessageBox.Show(arr1[2] + " " + arr2[2]); arr1[2] = 12; MessageBox.Show(arr1[2] + " " + arr2[2]); }Spruce
you are right, i corrected my answer to be more precise, using clone on arrays. You are absolutely right that "shallow copying an array of value types does not copy the values", but using clone on array does. I've tried to explain that, try it. ThanksSimonsimona
M
1

The copy constructor is used to initialize the new object with the previously created object of the same class. By default compiler wrote a shallow copy. Shallow copy works fine when dynamic memory allocation is not involved because when dynamic memory allocation is involved then both objects will points towards the same memory location in a heap, Therefore to remove this problem we wrote deep copy so both objects have their own copy of attributes in a memory. In order to read the details with complete examples and explanations you could see the article C++ constructors.

Metry answered 8/2, 2014 at 13:11 Comment(0)
R
1

To add just a little more for confusion between shallow copy and simply assign a new variable name to list.

"Say we have:

x = [
    [1,2,3],
    [4,5,6],
    ]

This statement creates 3 lists: 2 inner lists and one outer list. A reference to the outer list is then made available under the name x. If we do

y = x

no data gets copied. We still have the same 3 lists in memory somewhere. All this did is make the outer list available under the name y, in addition to its previous name x. If we do

y = list(x)

or

y = x[:]

This creates a new list with the same contents as x. List x contained a reference to the 2 inner lists, so the new list will also contain a reference to those same 2 inner lists. Only one list is copied—the outer list. Now there are 4 lists in memory, the two inner lists, the outer list, and the copy of the outer list. The original outer list is available under the name x, and the new outer list is made available under the name y.

The inner lists have not been copied! You can access and edit the inner lists from either x or y at this point!

If you have a two dimensional (or higher) list, or any kind of nested data structure, and you want to make a full copy of everything, then you want to use the deepcopy() function in the copy module. Your solution also works for 2-D lists, as iterates over the items in the outer list and makes a copy of each of them, then builds a new outer list for all the inner copies."

source: https://www.reddit.com/r/learnpython/comments/1afldr/why_is_copying_a_list_so_damn_difficult_in_python/

Recession answered 12/1, 2017 at 6:10 Comment(0)
E
0

Adding to all the above definitions, one more and most commonly used deep copy, is in the copy constructor (or overloading assignment oprator) of the class.

Shallow copy --> is when you are not providing copy constructor. Here, only the object gets copied but not all the members of the class are copied.

Deep copy --> is when you have decided to implement copy constructor or overload assignment in your class and allows copying all the members of the class.

MyClass& MyClass(const MyClass& obj) // copy constructor for MyClass
{
          // write your code, to copy all the members and return the new object
}
MyClass& operator=(const MyClass& obj) // overloading assignment operator,
{
          // write your code, to copy all the members and return the new object
}
Errecart answered 20/12, 2013 at 19:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.