Questions tagged [deep-copy]

A deep copy of an object is a separate, fully independent duplicate of that object, such that any references / pointers it holds to other objects refer to deep copies of those to which the original's refer. Use this tag for questions regarding implementing or using deep copying methods.

Deep copying is a process by which an object is fully duplicated to create a copy that is wholly independent of the original. This differs from shallow copying with respect to the treatment of references / pointers to other objects: deep copying makes a deep copy of each referenced object, too, whereas shallow copying copies only the references. Deep copying is "deep" in the sense that it traverses the object's reference graph all the way to the bottom.

Related tags

Links

1483 questions
19
votes
3 answers

Shallow copy and deep copy in C

I tried googling this but only objected oriented languages pop up as results. From my understanding a shallow copy is copying certain members of a struct. so lets say a struct is typedef struct node { char **ok; int hi; int yep; …
ShadyBears
  • 3,955
  • 13
  • 44
  • 66
19
votes
3 answers

Java HashMap - deep copy

I am just trying to find out the best solution how to make a deep copy of HashMap. There are no objects in this map which implement Cloneable. I would like to find better solution than serialization and deserialization.
Smolda
  • 882
  • 5
  • 13
  • 34
18
votes
2 answers

Ruby on Rails deep copy/ deep clone of object and its attributes

I would like to do a deep copy on objects including all the attributes. The experiment_old is has 10 trials. And I want to copy everything to experiment_new with the 10 trials. experiment_old should also keep the 10 trial info. However, all the…
shci
  • 195
  • 1
  • 1
  • 7
18
votes
5 answers

TypeError: cannot deepcopy this pattern object

Trying to understand this error in my "Variable" class. I was hoping to store a sre.SRE_Pattern in my "Variable" class. I just started copying the Variable class and noticed that it was causing all my Variable class instances to change. I now…
Marcus Jones
  • 960
  • 3
  • 11
  • 27
18
votes
7 answers

Remove shared references in list-of-list?

Ok, let me explain the problem with a simple example: l = [[0]]*3 # makes the array [[0], [0], [0]] l[0][0] = 42 # l becomes [[42], [42], [42]] from copy import deepcopy m = deepcopy(l) # m becomes [[42], [42], [42]] m[0][0] = 2 #…
Benoît P
  • 3,179
  • 13
  • 31
18
votes
5 answers

Copy constructor: deep copying an abstract class

Suppose I have the following (simplified case): class Color; class IColor { public: virtual Color getValue(const float u, const float v) const = 0; }; class Color : public IColor { public: float r,g,b; Color(float ar, float ag, float…
erik
  • 1,238
  • 3
  • 12
  • 23
17
votes
2 answers

Deep copy of PHP array of references

So $array is an array of which all elements are references. I want to append this array to another array called $results (in a loop), but since they are references, PHP copies the references and $results is full of identical elements. So far, the…
Chad
  • 2,064
  • 1
  • 22
  • 29
17
votes
7 answers

Call default copy constructor from within overloaded copy constructor

I need to write a copy constructor that deep copies the contents of a std::shared_ptr. However, there are a bunch of variable int a, b, c, d, e; also defined in the class. Is there a way to generate the default copy constructor code (or call the…
Alan Turing
  • 12,223
  • 16
  • 74
  • 116
17
votes
3 answers

Why deepcopy of list of integers returns the same integers in memory?

I understand the differences between shallow copy and deep copy as I have learnt in class. However the following doesn't make sense import copy a = [1, 2, 3, 4, 5] b = copy.deepcopy(a) print(a is b) print(a[0] is…
Gerald Lee
  • 179
  • 1
  • 1
  • 3
17
votes
2 answers

deepcopy and python - tips to avoid using it?

I have a very simple python routine that involves cycling through a list of roughly 20,000 latitude,longitude coordinates and calculating the distance of each point to a reference point. def compute_nearest_points( lat, lon, nPoints=5 ): …
si28719e
  • 2,135
  • 5
  • 20
  • 22
16
votes
4 answers

Is there an easy way to copy the TDictionary content into another?

Is there a single method or easy way how to copy one TDictionary content into another ? Let's say I have the following declarations type TItemKey = record ItemID: Integer; ItemType: Integer; end; TItemData = record Name: string; …
Martin Reiner
  • 2,167
  • 2
  • 20
  • 34
16
votes
4 answers

Deep copying data structures in golang

I want to duplicate an instance of a data structure. Since go does not have any builtins, I am using a third party library: https://github.com/emirpasic/gods. For example, I may try use deep copy with a hash set. var c, d hashset.Set c =…
Mayoi
  • 315
  • 1
  • 2
  • 6
16
votes
8 answers

Is clone() in java shallow copy?

Is clone() in java a shallow copy? Eventually this gets to the clone() method of Object (the uppermost class), which creates a new instance of the same class as the object and copies all the fields to the new instance (a "shallow…
Josh Morrison
  • 7,488
  • 25
  • 67
  • 86
15
votes
4 answers

Alternative of strcpy in c++

In C i used strcpy to make a deep copy of a string, but is it still 'fine' to use strcpy in C++ or are there better alternatives which i should use instead ?
Aerus
  • 4,332
  • 5
  • 43
  • 62
15
votes
4 answers

Is pass-by-value/reference equivalent to making a deep/shallow copy, respectively?

To reword the question in case someone types it into the search bar differently: Is pass-by-value the same as making a deep copy, and is pass-by-reference the same as making a shallow copy? If not, what is the difference? In Python, the language I'm…