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
15
votes
4 answers

Does a copy constructor/operator/function need to make clear which copy variant it implements?

Yesterday I asked a question about copying objects in C#, and most answers focussed on the difference between deep copy and shallow copy, and the fact that it should be made clear which of both copy variants a given copy constructor (or operator, or…
Dimitri C.
  • 21,861
  • 21
  • 85
  • 101
15
votes
2 answers

Does Enumerable.Repeat() do a deep copy?

If I use the following: var myList = Enumerable.Repeat(myCustomObject, 2); Will the Second element in the list be a deep copy of the first one? Note: myCustomObject can be any Object Edit: Could you also please let me know the potential use of…
Mahesh Velaga
  • 21,633
  • 5
  • 37
  • 59
15
votes
1 answer

Python: RuntimeError: super-class __init__() of %S was never called

I tried to do some operation (setParent) on an object in Python (an instance of a class which inherits from a different class - to be specific, QtGui.QLabel), but during runtime the above error was raised. The object itself has had some fields with…
Tomer
  • 189
  • 1
  • 3
  • 11
15
votes
7 answers

C# Automatic deep copy of struct

I have a struct, MyStruct, that has a private member private bool[] boolArray; and a method ChangeBoolValue(int index, bool Value). I have a class, MyClass, that has a field public MyStruct bools { get; private set; } When I create a new MyStruct…
3Pi
  • 1,814
  • 3
  • 19
  • 30
15
votes
5 answers

In Javascript, when performing a deep copy, how do I avoid a cycle, due to a property being "this"?

I have some library code that is cycling endlessly on me. I'm not clear on how to best perform cycle detection and avoidance in javascript. i.e. there's no programmatic way of inspecting whether an object came from a "this" reference, is there?…
Aaron Fi
  • 10,116
  • 13
  • 66
  • 91
15
votes
3 answers

Is there a decent way of creating a copy constructor in python?

I realize questions quite similar to this have been asked, though not exactly this way. I'd like to have an optional argument for the constructor of my class that, if it is an instance of my class, will be copied. For example, something like (I…
carmenism
  • 1,087
  • 3
  • 12
  • 31
13
votes
5 answers

Copy object properties: reflection or serialization - which is faster?

I have two objects of the same type and need to copy property values from one object to another. There are two options: Use reflection, navigate through the properties of the first object and copy the values. Serialize the first object and…
net_prog
  • 9,921
  • 16
  • 55
  • 70
13
votes
9 answers

Creating an easy to maintain copy constructor

Consider the following class: class A { char *p; int a, b, c, d; public: A(const &A); }; Note that I have to define a copy constructor in order to do a deep copy of "p". This has two issues: Most of the fields should simply be copied. Copying…
Roux hass
  • 754
  • 5
  • 5
13
votes
1 answer

Deep Copy of Complex Third Party Objects/Classes

I'm have been working on a project to create PDF forms using PDFView4Net. While the library is generally good, the forms creator is primitive and lacking basic features (such as copy/paste, alignment, formatting, etc.) when working with form fields…
mslissap
  • 413
  • 4
  • 14
13
votes
1 answer

Strange "Member function not viable" error in templated linear algebra vector class

I'm implementing a templated vector class (not the data container, but the vector in the linear algebra sense), and I'm getting quite a few errors whenever I refer to rhs in my operator overloading. Also, my copy constructor doesn't seem to be…
13
votes
2 answers

Python: deepcopy does not work on user-defined classes?

In the following example I would expect deepcopy to create a copy of field and not just copy the reference. What happens here and is there an easy way around it? from copy import deepcopy class Test: field = [(1,2)] t1 = Test() t2 =…
Elrond1337
  • 424
  • 1
  • 5
  • 16
13
votes
2 answers

std::string copy constructor NOT deep in GCC 4.1.2?

I wonder if i misunderstood something: does a copy constructor from std::string not copy its content? string str1 = "Hello World"; string str2(str1); if(str1.c_str() == str2.c_str()) // Same pointers! printf ("You will get into the IPC hell very…
13
votes
1 answer

Deep version of sys.getsizeof

I want to calculate the memory used by an object. sys.getsizeof is great, but is shallow (for example, called on a list, it would not include the memory taken by the list's elements). I'd like to write a generic "deep" version of sys.getsizeof. I…
max
  • 49,282
  • 56
  • 208
  • 355
12
votes
1 answer

Python: deeply copy ast node tree

I'm trying to use deepcopy (from the copy module) to deeply copy a node tree from the ast module. This doesn't seem to work. I'm getting strange errors like TypeError: required field "name" missing from FunctionDef when I use the copied result (and…
Albert
  • 65,406
  • 61
  • 242
  • 386
12
votes
2 answers

Structure deep copy

This may be a very basic question but somehow it got me tricked... when I write test code, it seems to work, but something is going wrong in production. // Header file #define length 100 typedef struct testStr_t { int a; char b; char…
Kiran
  • 5,478
  • 13
  • 56
  • 84