Deep copy vs shallow copy

Possible duplicate:
What is the difference between deep copy and shallow copy?

What is the difference between a deep and shallow copy. What type of copy does the copy constructor do?

+45
c ++ clone
Apr 17 2018-10-10T00:
source share
3 answers

Shallow copy:

Some members of the copy may refer to the same objects as the original:

class X { private: int i; int *pi; public: X() : pi(new int) { } X(const X& copy) // <-- copy ctor : i(copy.i), pi(copy.pi) { } }; 

Here, the pi member of the original and copied object X will point to the same int .




Deep copy:

All members of the original are cloned (recursively, if necessary). No shared objects:

 class X { private: int i; int *pi; public: X() : pi(new int) { } X(const X& copy) // <-- copy ctor : i(copy.i), pi(new int(*copy.pi)) // <-- note this line in particular! { } }; 

Here, the pi member of the original and copied object X will point to different int objects, but both of them have the same value.




The default copy constructor (which is automatically provided if you do not provide one yourself) creates only small copies.

Correction: A few comments below correctly pointed out that it is wrong to say that the default copy constructor always always executes a shallow copy (or a deep copy, for that matter). Regardless of whether the designer of the type copy creates a shallow copy or a deep copy, or something in between , depends on the combination of behavior of each copy member; a member type copy constructor can be made to do whatever it wants in the end.

This section 12.8, clause 8 of the 1998 C ++ standard, talks about the above code examples:

An implicit copy constructor for class X performs a copy of its subobjects. [...] Each subobject is copied into a method corresponding to its type: [...] [I] f the subobject has a scalar type, a built-in assignment operator b.

+47
Apr 17 '10 at 8:50
source share
— -

A key example of this is an array of pointers to structures or objects (which change).

A shallow copy copies the array and maintains references to the source objects.

A deep copy will copy (clone) objects so that they have nothing to do with the original. Implicit in this is that the object itself is heavily copied. This is where it gets heavy because there is no real way to know if something has been deeply copied or not.

The copy constructor is used to initialize a new object with a previously created object of the same class. By default, the compiler wrote a shallow copy. A shallow copy works fine when dynamic memory allocation is not involved, because when allocating dynamic memory, both objects point to the same memory location on the heap, so to remove this problem we wrote a deep copy so that both objects have their own copy of the attributes in memory .

To read the details with full examples and explanations, you can see the article Constructors and destructors .

The default copy constructor is shallow. You can make your own copy constructors deep or shallow, depending on the situation. See C ++ Notes: OOP: Copy Constructors .

+10
Apr 17 '10 at 8:50
source share

A deep copy literally performs a deep copy. This means that if your class has some fields that are links, their values ​​will be copied, not the links themselves. If, for example, you have two instances of class A and B with fields of a reference type and are doing a deep copy, changing the value of this field to will not affect the value in B. And vice versa. Things are different from a shallow copy because only links are copied, so changing this field in the copied object will affect the original object.

What type of copy does the copy constructor do?

It depends on the implementation. This means that there are no strict rules, you can implement it as a deep copy or a shallow copy, however, as far as I know, it is common practice to implement a deep copy in the copy constructor. The default copy constructor does a shallow copy though.

+2
Apr 17 '10 at 8:56
source share



All Articles